CodeIgniter Forums

Full Version: Templating with Code Igniter
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Unknown[/eluser]
I'm very new to Code Igniter, so any help or suggestions here is appreciated.

I know I'm being a little picky, but I would rather not have to load my header, load my content view, and then load my footer every time I want to render a page on my site.

I typically use a "master page" approach, where I have one master template that just include()s the other views. I find that this approach makes it a lot easier to keep my template organized, rather than splitting up my XHTML templates into header/footer sections

Can anyone help me brainstorm some ways to accomplish a master page approach? I was thinking of possibly creating a class that extends Controller (myController for example) and somehow have it call include header and include footer. However, if I make a class that extends myController, I need myController to first call include header, then call whatever is in the extending class's index(), then call include footer.

So yeah, hopefully that makes sense. It's a little late and I've been battling with my laptop all day so I'm pretty tired. Again, any ideas are very much appreciated.

Thanks!
Mike

edit:
after searching the forums I've stumbled across http://williamsconcepts.com/ci/codeignit...index.html, and it looks very promising.

El Forum

[eluser]Sumon[/eluser]
I think there are one more way you can implement master page concept without CI template.
First of all lets consider a view file. It have header footer and body where body container varies for controller and functions. Now lets say we first assign body container value into a variable.
Code:
$data['main_container_view']=$this->load->view("member/registration_page");
now send this array to master template as
Code:
$this->load->view("masterpage/not_logged_in" , $data);
I hope it helps you to implement master page concept in CI.

El Forum

[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.

El Forum

[eluser]Filip_vh[/eluser]
Hi!

Well it worked out and it is what I'm looking for.

Unfortunately it seems to not load my css anymore now.

Any idea's on that?
probably cause my css and images are located in my templates folder. My views can't find them I think?

El Forum

[eluser]xwero[/eluser]
Filip_vh you probably using a relative url? because views can be reused in other places it's best to use an absolute url.

El Forum

[eluser]Filip_vh[/eluser]
Yep,
I just found it myself as well but thanks. I looked at the page source and realised my stupidity...
The css and images were in the templates folder while they should be in a different.
Only problem is that now it doesn't recognize my '$content' in my master page while I am passing it through.
ah well...

I'll figure that one out as well soon enough. probably something stupid anyway ...

If I can't find it ... I'll be back Big Grin

Edit: I solved it. Thanks for the help guys! Big Grin

El Forum

[eluser]Bramme[/eluser]
Personally, I love the template library (by Colin Williams) for this kind of thing.

http://ellislab.com/forums/viewthread/88452/