Reputation: 2311
I am trying to include a header in my application.I created a controllers site
and defined a function header
in it, which renders the data variables to views/header.php
However, when i try to access myUrl/site/header
all varialble work fine but when i create another function in site controller index
and include views/header
in that, and i run site/index
then i get Undefined variable notices in the header.
Any Idea??
class site extends CI_Controller {
function header()
{
$data =array('name'=>'foo');
$this->load->view('includes/header',$data);
}
function index()
{
$this->load->view('includes/index');
}
}
INDEX.PHP
$this->load->view('includes/header');
Upvotes: 2
Views: 16827
Reputation: 2311
Got it Solved!!!!
Wrote a new helper and returned the data from there, then passed the data to view using load->view('includes/header',$data);
Upvotes: 0
Reputation:
This is not the correct way of doing it. Basically i do it like Rafiqunnabi except i don't load views within views.
For e.g:
public function index()
{
$this->load->view('include/header');
$this->load->view('home');
$this->load->view('include/footer');
}
Upvotes: 0
Reputation: 1605
If you want to keep separate segments(e.g header, footer, navigation bar etc) of your Html pages to separate view files, I think it's better to do this in the following way
keep the contents you want to be header to a file name header.php(or anything else you like) to views folder. Suppose, the contents of the header.php is
<html>
<head>
<title>This is title</title>
</head>
<body>
Another file named footer.php contains the following
</body>
</html>
Another file my_view.php in view folder that will hold the contents of that page. Sample contents are
<?php $this->load->view('header');?>
<h1>Welcome</h1>
<p>Main contents here</p>
<?php $this->load->view('footer');?>
Now, if you load the my_view from your controller function, the entire HTML page will be rendered.
function my_controller_function(){
$this->load->view('my_view');
}
Upvotes: 2