话不多说,直接代码,保存为libraries
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Layout
{
var $obj;
var $layout;
function Layout($layout = "layouts")
{
$this->obj =& get_instance();
$this->layout = $layout;
}
function setLayout($layout)
{
$this->layout = $layout;
}
function view($view, $data=null, $return=false)
{
$data['content'] = $this->obj->load->view($view,$data,true);
if($return)
{
$output = $this->obj->load->view($this->layout,$data, true);
return $output;
}
else
{
$this->obj->load->view($this->layout,$data, false);
}
}
}
?>
然后呢 在view下面创建你的layouts.php或者其它(用setLayout指定)
layout.php中 子视图的变量名叫 $content
即:
<html><head></head>
<body>
<h2>Layout</h2>
<div class="container">
<?php echo $content;?>
</div>
<p>footer</p>
</body>
</html>
controller里调用
$this->load->libraries('layout');
$this->layout->view('my/viewfile' , array('title' => 'page title'));''跟$this->load->view用法一样。