Welcome Guest, Not a member yet? Register   Sign In
Templating with Code Igniter
#3

[eluser]fesweb[/eluser]
Here's a simplified version of what I've just (finally) arrived at...

Master template (views/templates/master_page):
Code:
<html>
<body>
  <div id="main-content">
   &lt;?php if($content_main) echo $content_main; ?&gt;
  </div>
&lt;/body&gt;
&lt;/html&gt;

In whatever controller you are working with:
Code:
// get  some data
$data = $some_query->result();

// %%%%% the IMPORTANT bit %%%%%
// write the results to a variable (string), rather than output them to the browser
// to do that you must pass TRUE as the third parameter when loading the view
$output['content_main'] = $this->load->view('list_partial', $data, TRUE);

// then load the master page view, passing the html with $output
$this->load->view('templates/master_page', $output);
So, we write our actual content to a variable as a string of html, then we can echo that html in the master template.

This is undocumented on the "Views" page of the user guide, but I learned (afterward) that it's covered on the loader page.

You can do this with as many sections as you like, and load different master pages depending on your needs (ex: templates/master_2column, templates/master_3column, etc).

Master:
Code:
&lt;html&gt;
&lt;body&gt;
  <div id="nav-content">
   &lt;?php if($content_nav) echo $content_top; ?&gt;
  </div>
  <div id="main-content">
   &lt;?php if($content_main) echo $content_main; ?&gt;
  </div>
  <div id="sidebar">
   &lt;?php if($content_sidebar) echo $content_sidebar; ?&gt;
  </div>
&lt;/body&gt;
&lt;/html&gt;
Controller:
Code:
$data = $some_query->result();
$output['content_nav'] = $this->some_model->make_nav();
$output['content_main'] = $this->load->view('list_partial', $data, TRUE);
$output['content_sidebar'] = 'whatever kind of thing you like';
$this->load->view('templates/master_page', $output);

Hope that helps.


Messages In This Thread
Templating with Code Igniter - by El Forum - 08-19-2008, 09:56 PM
Templating with Code Igniter - by El Forum - 08-19-2008, 11:32 PM
Templating with Code Igniter - by El Forum - 08-21-2008, 05:33 PM
Templating with Code Igniter - by El Forum - 08-22-2008, 07:47 AM
Templating with Code Igniter - by El Forum - 08-22-2008, 07:56 AM
Templating with Code Igniter - by El Forum - 08-22-2008, 08:01 AM
Templating with Code Igniter - by El Forum - 08-22-2008, 11:01 AM



Theme © iAndrew 2016 - Forum software by © MyBB