Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Header and footer in CodeIgniter

I really don't enjoy writing in every controller:

    $this->load->view('templates/header');     $this->load->view('body');     $this->load->view('templates/footer'); 

Is it possible to do, that header and footer would be included automatically and if we need to change it, we could also do that? How do you deal with that? Or it's not a problem in your opinion? Thanks.

like image 246
good_evening Avatar asked Mar 02 '12 21:03

good_evening


People also ask

How do I add a header and footer in CI 4?

How to include header footer. First of all, create a common file in the view folder. In this example, I have created a file name as template. php in /app/Views/innerpages directory.

HOW include external php file in codeigniter?

Then in the middle of the file edit the two variables $application_folder and $system_path and make sure you're using an absolute path instead of a relative one. Then in your external PHP script just include the external. php file and use the $CI global object to access the whole codeigniter: include '../../external.


1 Answers

Here's what I do:

<?php  /**  * /application/core/MY_Loader.php  *  */ class MY_Loader extends CI_Loader {     public function template($template_name, $vars = array(), $return = FALSE)     {         $content  = $this->view('templates/header', $vars, $return);         $content .= $this->view($template_name, $vars, $return);         $content .= $this->view('templates/footer', $vars, $return);          if ($return)         {             return $content;         }     } } 

For CI 3.x:

class MY_Loader extends CI_Loader {     public function template($template_name, $vars = array(), $return = FALSE)     {         if($return):         $content  = $this->view('templates/header', $vars, $return);         $content .= $this->view($template_name, $vars, $return);         $content .= $this->view('templates/footer', $vars, $return);          return $content;     else:         $this->view('templates/header', $vars);         $this->view($template_name, $vars);         $this->view('templates/footer', $vars);     endif;     } } 

Then, in your controller, this is all you have to do:

<?php $this->load->template('body'); 
like image 87
landons Avatar answered Sep 21 '22 21:09

landons