Reputation: 29
I have created a view with the name template.php with the following code.
<?php
$this->load->view('header');
$this->load->view($middle);
$this->load->view('footer');
?>
and in controller i use like
public function index()
{
$data['middle'] = 'home';
$this->load->view('template',$data);
}
So my question is how to pass session data like $data['session'] = $this->session->all_userdata();
to header.
I can pass the session data by loading each view in controller. but how in template file.?
Upvotes: 1
Views: 1286
Reputation: 344
You can use trick like passing array of data from controller and passing inner array again in your template.php view.
public function index()
{
$data['middle'] = 'home';
$this->load->view('template',array('data' => $data)); //trick to get whole data array in your template view.
}
then in your template view
<?php
$this->load->view('header',$data);
$this->load->view($middle);
$this->load->view('footer');
?>
Upvotes: 0
Reputation: 1823
Personally, I wouldn't include the header and footer from the view. How I achieve this, is to extend the Controller. Like this;
class MY_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function show_view($view, $data = array())
{
$this->load->view('header', $data);
$this->load->view($view, $data);
$this->load->view('footer', $data);
}
}
Now, you can load any view, and we pass the $data array to both the header and footer too.
$this->show_view('homepage', $data);
I hope this helps.
Upvotes: 0
Reputation: 14544
In any of your views you should be able to use the session helper.
echo $this->session->userdata('foo');
Upvotes: 1