CodeIgniter FAQ: Loading a View within a View
A lot of new CodeIgniter users have at one point asked, "How to load a view within another View?"
To load a view within another view. We can also use the same method we used in the controller to load the "primary" view.
<?php $this->load->view('header');?>
<div>
<p>This is the content</p>
</div>
<?php $this->load->view('footer');?>
Some coders might desire not to put $this->load->view() in the view. An alternative is to create a helper for loading views.
// load_view_helper.php
if ( ! function_exists('load_view'))
{
function load_view($view, $vars = '', $return = FALSE)
{
$CI =& get_instance();
return $CI->load->view($view, $vars, $return);
}
}
Add this to the helper array:
$autoload['helper'] = array('load_view');
And you're all set! To use:
<?php load_view('header');?>
<div>
<p>This is the content</p>
</div>
<?php load_view('footer');?>
There are still tons of alternatives out there. One that is recommended is using Colin William's Template Library. Our example is one of the simplest.
Categories: How To, Web Development
Tags: codeigniter, php, codeigniter faq
1 Comments
Annie
That’s a nice helper! I might use this in my current project.
November 10th 2009