CodeIgniter Forums
Best way to load header and footer once globally? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: Best Practices (https://forum.codeigniter.com/forum-12.html)
+--- Thread: Best way to load header and footer once globally? (/thread-71825.html)



Best way to load header and footer once globally? - HarrysR - 09-28-2018

I was wondering what is the best way to load header and footer files globbaly once, in order to pass data from db in the template. For example, urls, meta descriptions and keywords etc. which will be controlled from the db.

I found this while i was searching the internet: https://stackoverflow.com/questions/9540576/header-and-footer-in-codeigniter
but i don't know if someone has a better idea.

For now i load the files and pass data as usual in every controller like:

$this->load->view('templates/header', $data);
$this->load->view('pages/[page_name]', $data);
$this->load->view('templates/footer', $data);

etc... etc...

This is fine for small scale sites with one or two controllers. What if i have more?!

Any suggestions on that?

Thanks!


RE: Best way to load header and footer once globally? - Wouter60 - 09-28-2018

Create a MY_Controller in the application/core folder.

Inside that controller, create this function:
PHP Code:
protected function render_page($view,$data)
{
 
  $this->load->view('templates/header'$data);
 
  $this->load->view($view$data);
 
  $this->load->view('templates/footer'$data);


Let all your controllers extend the MY_Controller.
Then, to load a page with header and footer:
PHP Code:
$this->render_page('pages/page_name',$data); 



RE: Best way to load header and footer once globally? - HarrysR - 10-02-2018

(09-28-2018, 09:43 AM)Wouter60 Wrote: Create a MY_Controller in the application/core folder.

Inside that controller, create this function:
PHP Code:
protected function render_page($view,$data)
{
 
  $this->load->view('templates/header'$data);
 
  $this->load->view($view$data);
 
  $this->load->view('templates/footer'$data);


Let all your controllers extend the MY_Controller.
Then, to load a page with header and footer:
PHP Code:
$this->render_page('pages/page_name',$data); 

Well it worked! I also loaded a model in there and it works fine! 

Thanks for the suggestion!


RE: Best way to load header and footer once globally? - imabot - 10-04-2018

(10-02-2018, 07:27 AM)HarrysR Wrote:
(09-28-2018, 09:43 AM)Wouter60 Wrote: Create a MY_Controller in the application/core folder.

Inside that controller, create this function:
PHP Code:
protected function render_page($view,$data)
{
 
  $this->load->view('templates/header'$data);
 
  $this->load->view($view$data);
 
  $this->load->view('templates/footer'$data);


Let all your controllers extend the MY_Controller.
Then, to load a page with header and footer:
PHP Code:
$this->render_page('pages/page_name',$data); 

Well it worked! I also loaded a model in there and it works fine! 

Thanks for the suggestion!

Hum hum ... i've got the following error "Message: Class 'MY_Controller' not found". What am I doing wrong ? Should I include the class in each controller ?


RE: Best way to load header and footer once globally? - ignitedcms - 10-04-2018

I personally hate extending the core, as it make migrating to a new version a bit more troublesome.


RE: Best way to load header and footer once globally? - HarrysR - 10-04-2018

(10-04-2018, 09:26 AM)ignitedcms Wrote: I personally hate extending the core, as it make migrating to a new version a bit more troublesome.

So do you have any other suggestions?!


RE: Best way to load header and footer once globally? - davis.lasis - 10-04-2018

I would suggest to make MY_Loader.php in application/core folder. That automagically extends load method and you don't need to extend any controller.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class MY_Loader extends CI_Loader {

function __construct() {
parent::__construct();

$CI =& get_instance();
$CI->load = $this;
}

// this is used in cms
public function template($template_name, $vars = array(), $return = TRUE){
$content = $this->view('header', $vars, $return);
$content .= $this->view('leftpanel', $vars, $return);
$content .= $this->view('rightpanel', $vars, $return);

if(is_array($template_name)) { //return all values in contents
foreach($template_name as $file_to_load) {
$content .= $this->view($file_to_load, $vars, $return);
}
}else{
$content .= $this->view($template_name, $vars, $return);
}

$content .= $this->view('footer', $vars, $return);

echo $content;
}
}

Then call it from controller like this:

$this->load->template('dashboard', $data);

And you can use multiple templates using array like:

$this->load->template(['dashboard', 'bottom_content'], $data);


RE: Best way to load header and footer once globally? - sintakonte - 10-08-2018

Phil Sturgeon once wrote a library which should fit your needs

take a look @ https://github.com/philsturgeon/codeigniter-template

it supports layouts, partials and so on

A documentation can be found under https://www.bioanalisisanimal.com/archivo/template_user_guide/


RE: Best way to load header and footer once globally? - HarrysR - 10-09-2018

(10-08-2018, 11:00 PM)sintakonte Wrote: Phil Sturgeon once wrote a library which should fit your needs

take a look @ https://github.com/philsturgeon/codeigniter-template

it supports layouts, partials and so on

A documentation can be found under https://www.bioanalisisanimal.com/archivo/template_user_guide/

Thank you! I will take a look at it!