- 视图views目录下新建目录_layouts存放layout文件
- libraries新建扩展Layout.php
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Layout
{
var $obj;
var $layout;
function __construct($layout = "member")
{
$this->obj =& get_instance();
$this->layout = '_layouts/' . $layout;
}
function setLayout($layout)
{
$this->layout = $layout;
}
function view($view, $data=null, $return=false)
{
$data['content_for_layout'] = $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);
}
}
}
- 我默认使用member作为layout的,因此在_layouts新建layout视图文件member.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>这是member的layout</title>
</head>
<body>
<?php echo $content_for_layout?>
</body>
</html>
- 控制器controller中使用如下
class Welcome extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('layout');
}
public function test_layout()
{
$data['test'] = '这是<a href="http://www.phpddt.com">PHP点点通</a>测试教程';
$this->layout->view('content',$data);
}
}
- views中新建content.php如下
<h2>这是content内容</h2>
<?php echo $test;?>
- 目录结构如下
Paste_Image.png
网友评论