CodeIgniter Forums
Header and Footer - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Header and Footer (/showthread.php?tid=56558)



Header and Footer - El Forum - 01-01-2013

[eluser]cPage[/eluser]
By extending the Core ?

Code:
class MY_Controller extends CI_Controller
{
private $header = 'header';
private $footer = 'footer';

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

protected function set_header($header)
{
  $this->header = $header;
}

protected function set_footer($footer)
{
  $this->footer = $footer;
}

protected function page($view,&$data=NULL)
{
  $this->load->view($this->header);
  $this->load->view($view,$data);
  $this->load->view($this->footer);
}
}

/* Location: ./application/core/MY_controller.php */

And using it into controller... (example : /application/controllers/users)

Code:
class Users extends MY_Controller
{
public function index()
{
  $this->load->database();
  $this->load->model('user_model');
  $view = 'user/all';
  $data['users'] = $this->user_model->get_all();
  $this->page($view,$data);
}

public function add()
{
  $view = 'user/add';
  $this->page($view);
}

public function edit($data)
{
  $view = 'user/edit';
  $this->page($view,$data);
}
...
}
/* Location: ./application/controllers/users.php */

It will take views in the folder /application/views by default, but you can also set a different header or footer from a different location by using function set_header() or set_footer(). For example , if you want a different header and footer for the edit form where it is locate in a subfolder of the views folder. e.g (application/views/user/header.php).

Code:
public function edit($data)
{
  $this->set_header('user/header');
  $this->set_footer('user/footer');
  $view = 'user/edit';
  $this->page($view,$data);
}

Is this the right way ?


Header and Footer - El Forum - 01-03-2013

[eluser]bigbusty[/eluser]
You could just write a simple Template library, which would look cleaner to me. The controller is not a function library.


Code:
class Template
{
    private $views;
    private $ci;

    public function __construct()
    {
        parent::__construct();
        $this->ci =& get_instance();
    }

    public function set($name, $view, $data)
    {
        $this->views[$name] = $this->ci->load->view($view, $data, true);
        return $this;
    }

    public function load($master_template)
    {
        $this->ci->load->view($master_template, $this->views);
    }
}

In your controller you could than do:
Code:
public function index()
{
    $data = array(
        'header' => array('variable' => 'This is my header data'),
        'content' => array('variable' => 'This is my content data'),
        'footer' => array('variable' => 'This is my footer data')
    );

    $this->template->set('header', 'header/top_nav', $data['header'])
            ->set('content', 'content/main_page', $data['content'])
            ->set('footer', 'footer/main_footer', $data['footer'])
            ->load('template');
}

I think this would be more usable, as you can load x views dynamically into your master template. I'm using a more advanced template library. This should fit the learning purpose.


Header and Footer - El Forum - 01-03-2013

[eluser]cPage[/eluser]
First of all , thanks for your reply.

So if i understand clearly what you've designed the master_template should be :

Code:
<?php
echo $header;
echo $content;
echo $footer;

Right ?

.:edit:.

Anyway, i've ended up with this ...

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Users extends CI_Controller
{
private $data = array();

function __construct()
{
  parent::__construct();
  $this->load->library('session');
  $this->load->library('template');
  $this->load->library('common');
  $this->load->database();
  $this->load->model('user_model');
  
  if( !$this->common->user_logged_in() )
  {
   $this->logout();
  }
  
  $this->session->unset_userdata('message');
    
  $this->data = array('header' => array('title' => 'Users'),
            'footer' => array());
        
  $this->template->set('header', 'header', $this->data['header'])
            ->set('footer', 'footer', $this->data['footer']);        
}

public function logout()
{
  $this->common->logout();
}

public function index()
{
  $this->data = array('content' => array('users' => $this->user_model->get_all(),
                      'message'=>$this->common->get_message()),
            'nav' => array('active'=>'all'));

  $this->template->set('content', 'user/all', $this->data['content'])
          ->set('nav', 'user/nav', $this->data['nav'])
          ->load('m_template');
}

//-> Add and Insert

public function add()
{
  $this->data = array('header' => array('title' => 'Add a user'),
            'nav' => array('active'=>'add'),
            'content' => array());
            
  $this->template->set('header', 'header', $this->data['header'])
          ->set('nav', 'user/nav', $this->data['nav'])
          ->set('content', 'user/add', $this->data['content'])
          ->load('m_template');
}

public function insert()
{
  $this->user_model->insert($this->input->post());
  $this->common->set_message('alert alert-success','User :  ['.$this->input->post('username').'] have been added.');
  $this->index();
}

//-> Edit and Update

public function edit($id)
{
  $this->data = array('header' => array('title' => 'Edit a user'),
            'nav' => array('active'=>'edit'),
            'content' => array('user'=>$this->user_model->get_user($id)));
            
  $this->template->set('header', 'header', $this->data['header'])
          ->set('nav', 'user/nav', $this->data['nav'])
          ->set('content', 'user/edit', $this->data['content'])
          ->load('m_template');
}

public function update()
{
  $email = $this->input->post('email');
  $this->user_model->update($this->input->post(),array('email' => $email));
  $this->common->set_message('alert alert-success','The user : ['.$this->input->post('username').'] have been updated!');
  $this->index();
}

//-> Delete
public function delete($id)
{
  $user = $this->user_model->get_user($id);
  if($user)
  {
   $this->common->set_message('alert alert-success','The user :  ['.$user->username.'] have been deleted.');
   $this->user_model->delete($id);
  }
  $this->index();
}
}
/* Location: ./application/controllers/users.php */



Header and Footer - El Forum - 01-04-2013

[eluser]bigbusty[/eluser]
Yea, you can use it like that. This class was just an idea of how you could integrate a simple templating engine. Of course you can extend it with more functionality, like title management, assets management (loading your .css or .js dynamically into your template), meta tag management, loading multiple views into a region, messaging, variable parsing, changing your themes dynamically and so on... as you can see a template library can be a beast.

I would extend it as you need more features, or rather choose a complete template library like phil sturgeons one. I use my own, because Phils is too big for my purpose.