3

I have worked with a custom php framework and django templates in the past, both of which support what I can describe as "base templates". Meaning you have a file where you have your headers and footers and a div lets say with the id "content". Now everything in your view files contain only the portion of code to fill that "content" div, the headers and footers are put in place by that base template.

How can I implement this sort of a "base layout" in codeigniter. And if that's not possible then how can I tell all my views to include a header.php and a footer.php file at the top and bottom of all my views respectively without having to add those includes manually to each view?

tereško
  • 58,060
  • 25
  • 98
  • 150

3 Answers3

4

Thanks for the answers guys but I decided to go with the following answer that was given by jruzafa in this question:

How to Deal With Codeigniter Templates?

Since it does exactly what I wanted and doesn't require any additional extensions:

I've tried several ways to do codeigniter templates and the way that I stay is the fastest and simplest, is as follows.

In controller:

>     //Charge the view inside array
>     $data['body'] = $this->load->view('pages/contact', '', true);
> 
> 
>     //charge the view "contact" in the other view template
>     $this->load->view('template', $data);

In view template.php:

> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
> "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html
> xmlns="http://www.w3.org/1999/xhtml" xml:lang="es">  <head> 
>     <title>Template codeigniter</title>  </head>  <body> 
>     <div> 
>         <?=$body?>
>     </div> 
>     <div class="clear"></div> 
>     <div>Footer</div> 
>     </div>  </body>  </html>

$body is the view contact.

Community
  • 1
  • 1
  • I don't know. When I use this method, all the variables from the controller get lost and I get lots of 'undefined variable' messages. – russellmania Mar 27 '14 at 04:03
0

Not sure I understood you completely, but you could make a view ('base_view') and then you could supply it with content via variables while loading common views from the base view itself. Like this:

Controller part:

$data['content'] = "<div>My content</div>";
$this->load->view('base_view', $data);

Base_view part:

$this->load->view('header');
echo $content;
$this->load->view('footer');

Hope this helps.

Shomz
  • 37,421
  • 4
  • 57
  • 85
0

Here's my favorite CodeIgniter template implementation. We use it in most of our projects. Very easy to understand and implement.

http://maestric.com/doc/php/codeigniter_template

davidethell
  • 11,708
  • 6
  • 43
  • 63