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.
This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.
The active forums are here.
November 11, 2010 1:04am
Subscribe [6]#1 / Nov 11, 2010 1:04am
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.
#2 / Nov 11, 2010 3:49am
Well, there are different ways to achief this.
Take a look at two examples:
http://codeigniter.com/wiki/Header_and_Footer_and_Menu_on_every_page_-_haloace/
http://codeigniter.com/wiki/Header_and_Footer_and_Menu_on_every_page_-_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
#3 / Nov 11, 2010 6:58am
Here is how I do that:
1. The template library
Create a template.php in your system/applications/libraries/ folder.
template.php =
<?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):
$this->load->library('template');or you can autoload it in your config/autoload.php:
$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:
<html>
<head>
<!-- add your javascript includes here -->
<!-- add your css includes here -->
<!-- etc -->
</head>
<body>
<div id='header'>
<!-- include your header view here -->
<?php $this->load->view('header'); ?>
</div>
<div id='whateverworks'>
<?= $contents ?>
</div>
<div id='footer'>
<!-- include your footer view here -->
<?php $this->load->view('footer'); ?>
</div>
</body>
</html>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:
$this->load->view('blog/show_post's);you can now use
$this->template->load('template', 'blog/show_posts');or
$this->template->load('template', 'blog/show_posts', $data); // :blush: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:
$this->set('contents', $this->CI->load->view($view, $view_data, TRUE));Hope that helps!
P.
#4 / Jan 26, 2011 7:46am
thanks for this, really usefull
but i have a question
in my header.php, which is included in the main template, i have:
Wellcome, <?php echo $user; ?>and because i have this header on every page, i want to extend CI_Controller with MY_Controller. And in MY_Controller i have:
$user = $this->auth->get_user();
$data['user'] = $user->username;in my template.php file, i also have:
<?php $this->load->view('header'); ?>but the result is ... “Undefined variable: User”
any ideas how to include something globally, with MY_Controller and this simple template library ?
#5 / Jan 26, 2011 10:20am
After data array is built, do not pass $data to this load view.
$this->load->vars($data);
// Now load your view or template…InsiteFX
#6 / Jan 27, 2011 12:44pm
Thanks, InsideFX!
Can this simple library be editted to use the template parser somehow ?
#7 / Jan 27, 2011 3:24pm
The $this->load->vars($data); is part of codeigniter.
CodeIginter keeps a global var cache of these variaibles.
InsiteFX
#8 / Feb 07, 2011 11:59am
Here is how I do that:
1. The template library
Create a template.php in your system/applications/libraries/ folder.
template.php =
<?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):
$this->load->library('template');or you can autoload it in your config/autoload.php:
$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:
<html> <head> <!-- add your javascript includes here --> <!-- add your css includes here --> <!-- etc --> </head> <body> <div id='header'> <!-- include your header view here --> <?php $this->load->view('header'); ?> </div> <div id='whateverworks'> <?= $contents ?> </div> <div id='footer'> <!-- include your footer view here --> <?php $this->load->view('footer'); ?> </div> </body> </html>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:
$this->load->view('blog/show_post's);you can now use
$this->template->load('template', 'blog/show_posts');or
$this->template->load('template', 'blog/show_posts', $data); // :blush: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:
$this->set('contents', $this->CI->load->view($view, $view_data, TRUE));Hope that helps!
P.
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?
#9 / Oct 25, 2011 11:22pm
Here is how I do that:
1. The template library
Create a template.php in your system/applications/libraries/ folder.
template.php =
<?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):
$this->load->library('template');or you can autoload it in your config/autoload.php:
$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:
<html> <head> <!-- add your javascript includes here --> <!-- add your css includes here --> <!-- etc --> </head> <body> <div id='header'> <!-- include your header view here --> <?php $this->load->view('header'); ?> </div> <div id='whateverworks'> <?= $contents ?> </div> <div id='footer'> <!-- include your footer view here --> <?php $this->load->view('footer'); ?> </div> </body> </html>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:
$this->load->view('blog/show_post's);you can now use
$this->template->load('template', 'blog/show_posts');or
$this->template->load('template', 'blog/show_posts', $data); // :blush: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:
$this->set('contents', $this->CI->load->view($view, $view_data, TRUE));Hope that helps!
P.
If the plan is to use two different headers (one for user logged in, one for logged out), would it be proper to do an if statement directly in the views/template.php file in order to determine which header to load?
i.e:
if (user logged in) load header1
else load header2
Or if anyone knows of a better way, greatly appreciated.