CodeIgniter Forums

Full Version: Sharing data between view partials
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]Isuka[/eluser]
Hi,

When I build an app, I usually create a master view like this :
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
&lt;html &gt;
&lt;head&gt;
&lt;title&gt;&lt;?php echo title; ?&gt;&lt;/title&gt;  
&lt;?php echo $view_css; ?&gt;
&lt;/head&gt;
&lt;body&gt;
<div id="header">
<h1>&lt;?php echo $title; ?&gt;</h1>
</div>
<div id="content">
&lt;?php echo $content; ?&gt;
</div>
<div id="sidebar">
&lt;?php echo $sidebar; ?&gt;
</div>
<div id="footer">
&copy; mygreatwebsite.com
</div>
&lt;/body&gt;
&lt;/html&gt;

In my controller I make something like this :
Code:
class Welcome extends Controller
{
  function Welcome()
  {
    parent::Controller();    
  }    
  function index()
  {
    $data = array(
      'title'=>'My Great Title',
      'content'=>$this->load->view('content_partial_view', '', TRUE),
      'sidebar'=>$this->load->view('sidebar_partial_view', '', TRUE)
    );
    $this->load->view('master', $data);
  }
}
In my actual case, I have access to $title only in my master view but not in my content and sidebar partials. What if I want to have access to $title in my content view ? I need to make another array with the same title data specificaly for the content view. And the same goes for the sidebar view.

So I'm wondering if it's possible to have a global $data accessible for all my views (master, content and sidebar).

(I used to make partial with the great View Library by coolfactor before, but there's not update since July 2007, so I try to use the default CI View Library now Tongue)

Thanks for any helpful information Smile

El Forum

[eluser]darrenm[/eluser]
I use a slightly different approach. I abstract my view loading into a custom library called Page.

This lib has functions for adding and removing style sheets and javascripts and a few other things. It then has a function called render_page() which loads all the various views required

Code:
function render_page($view,$data) {
  $this->CI->load->view('_header',$data);
  $this->CI->load->view($view,$data);
  $this->CI->load->view('_footer',$data);
}

you can then call this from your controller like so:

Code:
$data['docTitle'] = 'My document title';
$data['content'] = 'Page content';
$data['something'] = 'else';
$this->page->render_page('my_view_file',$data);

This keeps all $data in one array and has lots of scope for handling other common tasks (I use it in conjunction with a navigation generator for example).

Hope that helps

El Forum

[eluser]Isuka[/eluser]
Your approach is like the View Library I used before and it works pretty well, so I think I'm going to not change my habits and stay with it. Maybe in CI 2.0 I will have a view library I want to use Tongue

El Forum

[eluser]John_Betong[/eluser]
Hi,

>>> When I build an app, I usually create a master view like this :
&nbsp;
Try this instead
Code:
class Welcome extends Controller
{
  function Welcome()
  {
    parent::Controller();    
  }    
  function index()
  {
    $data = array();
    $data['title'] = 'My Great Title';

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

    // See Xwero's better way of using array data below
    $output = $this->load->view('master', $data, TRUE);
    echo $output;

  }//endfunc

}//endclass
&nbsp;
&nbsp;&nbsp;

El Forum

[eluser]xwero[/eluser]
If you use load->vars the title will be 'global'
Code:
$this->load->vars(array('title'=>'My Great Title'));
$data = array(
      'content'=>$this->load->view('content_partial_view', '', TRUE),
      'sidebar'=>$this->load->view('sidebar_partial_view', '', TRUE)
    );
$this->load->view('master', $data);
But you have to be careful not to overwrite variables when adding partial related data.

El Forum

[eluser]xwero[/eluser]
John a tip : don't give all your arrays the same name. In the example you show the title key will be passed on to the master view too which is not needed.

load->vars does the same thing, added for the people who pay attention Wink

El Forum

[eluser]Isuka[/eluser]
@John_Betong : Great tips, didn't think about that Smile It work well with my exemple but with a more complex controller it might be tricky.

@xwero : I never used the load->vars method before, but it's exactly the method I need for what I want to do.

Thanks !

El Forum

[eluser]John_Betong[/eluser]
&nbsp;
&nbsp;
Hi @Xwreo and @Isuka,
&nbsp;
I have amended my original post and removed $data where it was unnecessary.
&nbsp;
&nbsp;
@Isuka in complex controllers it does get tricky, just ensure all your variables are declared before loading the partial views otherwise some weird errorrs appear which are not easy to track down.
&nbsp;
Keep your code clean and simple so it is easy to read later and to easily add
Code:
echo '$problem_variable ---&gt;' .$problem_variable .<---'; die;
&nbsp;
&nbsp;
By the way, I am from the old school (with umpteen T-shirts) that shy away from variables that are global and prefer using locals instead. Far easier to trace problems.
&nbsp;
&nbsp;

El Forum

[eluser]xwero[/eluser]
With you new code you are not adding the partials to the master view file.
Code:
$glob_partials['title'] = 'My Great Title';

$master['content'] = $this->load->view('content_partial_view', $glob_partials, TRUE);
$master['sidebar']  = $this->load->view('sidebar_partial_view', $glob_partials, TRUE);

$this->load->view('master', $master);
This code is more readable and flexible. Lets say there are some other variables needed for the content then you can do
Code:
$glob_partials['title'] = 'My Great Title';

$content = array_merge($glob_partials,array('body'=>'long text'));

$master['content'] = $this->load->view('content_partial_view', $content, TRUE);
$master['sidebar']  = $this->load->view('sidebar_partial_view', $glob_partials, TRUE);

$this->load->view('master', $master);
Do you see now how something simple like naming arrays differently can make your code better.

El Forum

[eluser]John_Betong[/eluser]
&nbsp;
Hi Xwero,
&nbsp;
I like your first example it is easy to read and understand at a glance.
&nbsp;
Your second example is a wee bit too complex for me and not easy to read and understand at a glance.
&nbsp;
I will endeavour to utilise your first example.
&nbsp;
Many thanks for the tip.
&nbsp;
&nbsp;
Pages: 1 2