[eluser]TWP Marketing[/eluser]
[quote author="Piekarz" date="1347981476"]Hi, I am writing cause i got issue with including file in my views.
I got the following code in my view:
Code:
<section id="head">
<header id="main"></header><nav id="main"><?php require_once('/application/views/menu.php'); ?></nav>
</section>
It include file menu.php. I used it for no repeat the code in many of view's files.
The problem is that when i try to get some data from session in including file i can't use same variables like in my view's file.
My menu.php file:
Code:
echo $_SESSION['somedata'];
or
Code:
echo $this->session->sess_read('somedata');
How can I force in including files to use my CI variables like sessions?[/quote]
It is a better idea to read your session in the controller and pass the values via the view loader. This avoids a problem if your views are cached for display at a later time.
controller:
Code:
...
$data = array(
'somedata' => $this->session->get_userdata('somedata'),
...
):
...
$data['menu'] = $this->load->view('menu',$data,TRUE); // save the menu view as an HTML string
...
$this->load->view('page',$data); // output your final page (a template) to the browser
In the menu view:
Code:
...
echo $somedata; // this is the value of 'somedata' from your session
...
In your final view (I called it "page", but use the name of your view:
Code:
<section id="head">
<header id="main">
...
</header>
<nav id="main">
<?php
echo $menu; // this is the full menu view created in the controller
?>
</nav>
</section>
...