Welcome Guest, Not a member yet? Register   Sign In
Using view data from a Helper
#1

[eluser]kimon[/eluser]
Hi all,

first of all I would like to thank the CI Stuff for just a great framework, and of course the community here for the great job they do with helping people with their knowledge. Thank you both !!!


Now, I use CI for building CMS-eShop project. That project will be my pilot for all other similar projects that I will make, so I decided to invest on something that I could reuse.

My question:
I have a controller where I do simple things:
Code:
$data['body']= $body_content;
$data['sidebar'] = $sidebar_content;
$this->load->view('default.php', $data);

my default.php view is the following:
Code:
<div id="main">
  <div id="content">
    &lt;?php echo $body; ?&gt;
  </div>

  <div id="sidebar">
    &lt;?php echo $sidebar; ?&gt;
  </div>
</div>

is it possible to have a view like this
Code:
<div id="main">
  <div id="content">
    &lt;?php displayBody(); ?&gt;
  </div>

  <div id="sidebar">
    &lt;?php displaySidebar(); ?&gt;
  </div>
</div>


and the displayBody() and displaySidebar() function would be declared in a helper (or somewhere else) ?

Of course I tried to access them from inside a helper, but with no success.
#2

[eluser]Mike Ryan[/eluser]
Hi kimon,

Welcome to CI!

Using helpers in views would work, but it is not recommended. You could use one view file for each section, then load them all at the same time:
Code:
$body_data = ''; //whatever is in your body area
$sidebar_data = '';
$this->load->view('default');
$this->load->view('sidebar', $sidebar_data);
$this->load->view('body', $body_data);
$this->load->view('footer');
#3

[eluser]slowgary[/eluser]
I'm not sure what you're trying to do, but my situation might be similar. I wanted to have 1 main view, and not have a bunch of code in it. If you set the view() function's third parameter to TRUE it returns the markup to you, so you could do something like this:

Code:
//Controller

$data['body'] = $this->load->view('body', $body, TRUE);
$data['sidebar'] = $this->load->view('sidebar', $sidebar, TRUE);

$this->load->view('default.php', $data);


//View
<div id="main">
  <div id="content">
    &lt;?php echo $body; ?&gt;
  </div>

  <div id="sidebar">
    &lt;?php echo $sidebar; ?&gt;
  </div>
</div>

I'm assuming you have more processing to do with your $body and $sidebar and don't want to do them in the main view or the controller. If I'm wrong, give us more details on what your helper functions would do.
#4

[eluser]kimon[/eluser]
Ohhhh, that was fast responses, thank you guys !!!

Actually there is no any planned processing that I would like to do in those functions with the $body and $sidebar variables. Mostly are wrapper functions in order to hide the echo $var; statement from my template view. This is for future use if I would like to change the variable names or variable types (to an array or an object) or just do a little processing that might needed to be part of the view and not the controller.
#5

[eluser]TheFuzzy0ne[/eluser]
Welcome to the CodeIgniter forums!

You need to echo the value returned from the function:

Code:
&lt;?php echo displayBody(); ?&gt; # PHP 4+

&lt;?=displayBody()?&gt; # PHP 5+

However, I can't help thinking that you might be better off including a view rather than calling upon the helper function. If your helper contains mostly HTML and simple logic, then I'd highly recommend you put it in a view. You can then either load the contents of that view into a variable which you pass to your main template, or you can load the view directly from within your main template.

EDIT: Darn it. That's what happens when you leave a thread open for 45 minutes and don't refresh it before replying.
#6

[eluser]kimon[/eluser]
Ok, propably I have confused you with what I am try to do.

suppose that I have the following view
Code:
// view file
&lt;?php

function displayBody()
{
  echo $body;
}

function displaySidebar()
{
  echo $sidebar;
}

?&gt;


<div id="main">
  <div id="content">
    &lt;?php displayBody(); ?&gt;
  </div>

  <div id="sidebar">
    &lt;?php displaySidebar(); ?&gt;
  </div>
</div>

can I have those two function outside my view file in another .php file, possible a helper ?
#7

[eluser]TheFuzzy0ne[/eluser]
Yes, you should never define a function within your view. That's exactly what helpers are for. Smile However, I don't understand why you have a function that echoes a variable that you can echo directly from within you view anyway. Also, those function will not work, as neither of the variables are available in the global scope, even if you tried to import them with the "global" keyword.
#8

[eluser]kimon[/eluser]
I need the function so I can have loose coupling between the view and my controller, in such a way that the only coupling will be between my view and the 'helper' function, where this is the only place that the $body and $sidebar variable will be accessed. In the future, an extension to my controllers code or a modification will mean that I will have to change all the views that I have created so far, although the modification could have been backwards compatible using a 'helper' function.

For example, now I design my controller and my view and I create a $body variable that holds my page content on cms project. After a while some new requirements alters that design and I need to pass another structure to the view. In such a situation I will have to alter both controller's and view's code. If I had the ability for an external function to be used in the view, I will only changed that. So any view that I had, would have running like a charm.
#9

[eluser]TheFuzzy0ne[/eluser]
OK, I'm lost now... Where exactly does your helper function get it's data?
#10

[eluser]kimon[/eluser]
from the data that the controller passed to the view ...



on possible solution that I am currently examine is the following:


Code:
// controller
$content['body'] = 'Lorem Ipsum';
$data['content'] = $content;
$this->load->view('default', $data);


// view file
<div id="content">
  &lt;?php displayBody($data); ?&gt;
</div>



// helper file
function displayBody($content)
{
  echo $content['body'];
}




Theme © iAndrew 2016 - Forum software by © MyBB