引言:
$this->load->view('header'); 那么在视图header里面再来一个$this->load->view('menu');
显然这个会产生错误,以下是解决方案~~
$this->load->view('header'); 那么在视图header里面再来一个$this->load->view('menu');
显然这个会产生错误,以下是解决方案~~
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.