CodeIgniter Forums

Full Version: Loading Header and Footer in codeigniter
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]shankar ganesh[/eluser]
I am having two separate header.php and footer.php files. It should be used in all pages, i don't like to hardcode in each and every pages. How and where to place these two files in autoload.php, so it will appear in all pages.

El Forum

[eluser]smilie[/eluser]
Well, there are different ways to achief this.

Take a look at two examples:

http://codeigniter.com/wiki/Header_and_F...-_haloace/
http://codeigniter.com/wiki/Header_and_F...ge_-_jedd/

I am using Jedd's approach as my header and footer may change in case of user is logged in, or is not logged in. But if your header and footer will always be same - go with the Haloace's example.

Cheers,
Smilie

El Forum

[eluser]PeterGreffen[/eluser]
Here is how I do that:

1. The template library

Create a template.php in your system/applications/libraries/ folder.

template.php =

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class template {
        var $template_data = array();
        
        function set($name, $value)
        {
            $this->template_data[$name] = $value;
        }
    
        function load($template = '', $view = '' , $view_data = array(), $return = FALSE)
        {              
            $this->CI =& get_instance();
            $this->set('contents', $this->CI->load->view($view, $view_data, TRUE));            
            return $this->CI->load->view($template, $this->template_data, $return);
        }
}

/* End of file Template.php */
/* Location: ./system/application/libraries/template.php */


2. Load the library in you controller(s):

Code:
$this->load->library('template');

or you can autoload it in your config/autoload.php:

Code:
$autoload['libraries'] = array('template');

3. Create a new template.php file in your system/applications/views folder. The contents of that template.php can be something like this:

Code:
<html>
<head>
   <!-- add your javascript includes here -->
   <!-- add your css includes here -->
   <!-- etc -->
</head>

<body>

<div id='header'>
    &lt;!-- include your header view here --&gt;
    &lt;?php $this->load->view('header'); ?&gt;
</div>

<div id='whateverworks'>
   &lt;?= $contents ?&gt;
</div>

<div id='footer'>
    &lt;!-- include your footer view here --&gt;
    &lt;?php $this->load->view('footer'); ?&gt;
</div>

&lt;/body&gt;

&lt;/html&gt;

Everytime you load a view, you will load this template view and and another where the $content is. Here's how, in your Controller, instead of using:

Code:
$this->load->view('blog/show_post's);

you can now use

Code:
$this->template->load('template', 'blog/show_posts');
or
Code:
$this->template->load('template', 'blog/show_posts', $data); // :)

That will load the template view file with the view (blog/show_posts) included.

NOTE: make sure the variable name (here that is '$contents' in your template view, is the same as the one you use in the load() function in your template library in:

Code:
$this->set('contents', $this->CI->load->view($view, $view_data, TRUE));

Hope that helps!
P.

El Forum

[eluser]victorche[/eluser]
thanks for this, really usefull
but i have a question
in my header.php, which is included in the main template, i have:
Code:
<p>Wellcome, &lt;?php echo $user; ?&gt;</p>
and because i have this header on every page, i want to extend CI_Controller with MY_Controller. And in MY_Controller i have:
Code:
$user = $this->auth->get_user();
$data['user'] = $user->username;
in my template.php file, i also have:
Code:
&lt;?php $this->load->view('header'); ?&gt;
but the result is ... "Undefined variable: User"
any ideas how to include something globally, with MY_Controller and this simple template library ?

El Forum

[eluser]InsiteFX[/eluser]
After data array is built, do not pass $data to this load view.

Code:
$this->load->vars($data);
// Now load your view or template...

InsiteFX

El Forum

[eluser]victorche[/eluser]
Thanks, InsideFX!
Can this simple library be editted to use the template parser somehow ?

El Forum

[eluser]InsiteFX[/eluser]
The $this->load->vars($data); is part of codeigniter.
CodeIginter keeps a global var cache of these variaibles.

InsiteFX

El Forum

[eluser]Unknown[/eluser]
[quote author="PeterGreffen" date="1289494726"]Here is how I do that:

1. The template library

Create a template.php in your system/applications/libraries/ folder.

template.php =

Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class template {
        var $template_data = array();
        
        function set($name, $value)
        {
            $this->template_data[$name] = $value;
        }
    
        function load($template = '', $view = '' , $view_data = array(), $return = FALSE)
        {              
            $this->CI =& get_instance();
            $this->set('contents', $this->CI->load->view($view, $view_data, TRUE));            
            return $this->CI->load->view($template, $this->template_data, $return);
        }
}

/* End of file Template.php */
/* Location: ./system/application/libraries/template.php */


2. Load the library in you controller(s):

Code:
$this->load->library('template');

or you can autoload it in your config/autoload.php:

Code:
$autoload['libraries'] = array('template');

3. Create a new template.php file in your system/applications/views folder. The contents of that template.php can be something like this:

Code:
&lt;html&gt;
&lt;head&gt;
   &lt;!-- add your javascript includes here --&gt;
   &lt;!-- add your css includes here --&gt;
   &lt;!-- etc --&gt;
&lt;/head&gt;

&lt;body&gt;

<div id='header'>
    &lt;!-- include your header view here --&gt;
    &lt;?php $this->load->view('header'); ?&gt;
</div>

<div id='whateverworks'>
   &lt;?= $contents ?&gt;
</div>

<div id='footer'>
    &lt;!-- include your footer view here --&gt;
    &lt;?php $this->load->view('footer'); ?&gt;
</div>

&lt;/body&gt;

&lt;/html&gt;

Everytime you load a view, you will load this template view and and another where the $content is. Here's how, in your Controller, instead of using:

Code:
$this->load->view('blog/show_post's);

you can now use

Code:
$this->template->load('template', 'blog/show_posts');
or
Code:
$this->template->load('template', 'blog/show_posts', $data); // :)

That will load the template view file with the view (blog/show_posts) included.

NOTE: make sure the variable name (here that is '$contents' in your template view, is the same as the one you use in the load() function in your template library in:

Code:
$this->set('contents', $this->CI->load->view($view, $view_data, TRUE));

Hope that helps!
P.[/quote]

I'm trying the same code with a few changes... but it give me an error..
I follow all the first 3 steps, and of course i use 'contents' in both template.php in libraries and views..
it says

Severity: Notice

Message: Undefined variable: contents

Filename: views/template.php

Line Number: 16

one thing that i change is the
$this->template->load('template', 'blog/show_posts'); part
i change it with
$this->template->load('template','failure');

i put my failure.php in application/views and only consist on word "failure", but it don't work.. Did i miss something?