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.
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.
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.
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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With