Welcome Guest, Not a member yet? Register   Sign In
Best way to load header and footer once globally?
#1

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/9540...odeigniter
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!

//Life motto
if (sad() == true) {
     sad().stop();
     develop();
}
Reply
#2

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); 
Reply
#3

(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!

//Life motto
if (sad() == true) {
     sad().stop();
     develop();
}
Reply
#4

(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 ?
Reply
#5

(This post was last modified: 10-04-2018, 09:26 AM by ignitedcms.)

I personally hate extending the core, as it make migrating to a new version a bit more troublesome.
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply
#6

(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?!

//Life motto
if (sad() == true) {
     sad().stop();
     develop();
}
Reply
#7

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);
Reply
#8

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/archiv...ser_guide/
Reply
#9

(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/archiv...ser_guide/

Thank you! I will take a look at it!

//Life motto
if (sad() == true) {
     sad().stop();
     develop();
}
Reply




Theme © iAndrew 2016 - Forum software by © MyBB