Welcome Guest, Not a member yet? Register   Sign In
Header and Footer
#1

[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 ?


Messages In This Thread
Header and Footer - by El Forum - 01-01-2013, 08:00 PM
Header and Footer - by El Forum - 01-03-2013, 01:58 AM
Header and Footer - by El Forum - 01-03-2013, 10:40 AM
Header and Footer - by El Forum - 01-04-2013, 03:29 AM



Theme © iAndrew 2016 - Forum software by © MyBB