Loading Header and Footer in codeigniter - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: Loading Header and Footer in codeigniter (/showthread.php?tid=35784) |
Loading Header and Footer in codeigniter - El Forum - 11-10-2010 [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. Loading Header and Footer in codeigniter - El Forum - 11-11-2010 [eluser]smilie[/eluser] 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 Loading Header and Footer in codeigniter - El Forum - 11-11-2010 [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'); 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> 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'); 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. Loading Header and Footer in codeigniter - El Forum - 01-26-2011 [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, <?php echo $user; ?></p> Code: $user = $this->auth->get_user(); Code: <?php $this->load->view('header'); ?> any ideas how to include something globally, with MY_Controller and this simple template library ? Loading Header and Footer in codeigniter - El Forum - 01-26-2011 [eluser]InsiteFX[/eluser] After data array is built, do not pass $data to this load view. Code: $this->load->vars($data); InsiteFX Loading Header and Footer in codeigniter - El Forum - 01-27-2011 [eluser]victorche[/eluser] Thanks, InsideFX! Can this simple library be editted to use the template parser somehow ? Loading Header and Footer in codeigniter - El Forum - 01-27-2011 [eluser]InsiteFX[/eluser] The $this->load->vars($data); is part of codeigniter. CodeIginter keeps a global var cache of these variaibles. InsiteFX Loading Header and Footer in codeigniter - El Forum - 02-07-2011 [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: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 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> 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'); 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? |