总感觉CI自带的模板类用起来有点不方便,看了些网上其他的类似功能的库,都满足不了我的要求,最主要的模板嵌套很繁琐,于是自己动手写了个。
支持模块嵌套
支持Parse
支持指定layout和单个模块的文件及变量
使用说明
在views目录下建立每个controller对应的目录以及default目录,layout先在当前controller目录中查找文件,若没有
则在default中查找,如还没有,则抛出异常。
文件内容
layout.php
<div id="container">
{header}
{content}
{footer}
</div>
header.php
<h1>{subheader}<h1>
subheader.php
Welcome to CodeIgniter!<?php echo $hello; ?>
content.php
<div id="body"><code>application/controllers/welcome.php</code>{subcontent}</div>
subcontent.php
<p>If you are exploring CodeIgniter for the very first time.</p>
footer.php
<p>{subfooter}Page rendered in <strong>{elapsed_time}</strong> seconds</p>
sub.php
{thefooter}
常用方法
__construct($layout)构造函数,$layout为布局名称,默认为layout.php,
先在当前controller的目录中寻找,没有则到default中寻找
setSlot($path,$vars,$file)用于指定各个模块
$path为模块层级,如header/subheader表示header下的subheader
$vars为该模块中使用的变量
$file为该模块对应文件,如不指定则自动寻找与模块名相同的文件,如subheader.php
view($vars,$view,$return)用于显示布局
$vars为布局中使用的变量
$view未使用,留空
$return是否返回字符串,默认false,为true则返回字符串
其他
$parse是否使用Parse库,如不使用,模板中不能用{}表示变量,默认使用
$merge合并模板变量,如使用,在处理各子模块时会将setslot中的$vars,和view中的$vars合并使用,这样setslot可带vars参数,统一到view中指定,默认不使用
!每一个子模块都需要用setslot指定!
示例代码
class Welcome extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->library('layout');
$this->layout->setSlot('header');
$this->layout->setSlot('header/subheader',array('hello'=>'This is a layout library'));
$this->layout->setSlot('footer/subfooter',array(),'sub.php');
$this->layout->setSlot('footer');
$this->layout->setSlot('content/subcontent');
$this->layout->setSlot('content');
}
public function index()
{
$this->layout->view(array('hello'=>'hello layout!','thefooter'=>'I am a FOOTER MAN!!'));
}
}
下载download



[...] http://blog.lyphp.com/archives/478 CodeIgniter的模板布局库layout library [...]
Lyndon你好!
我安装了你的Layout library,很好用,谢谢你的辛苦劳动~建议你用github来发布你的模板系统,并且去ci论坛里面推荐给大家,这样才能更好地普惠大众噢.
我使用的ci是2.0.3,有这么两个小问题:一是sort函数中需要将”self::cmp”改为array(‘self’,'cmp’).第二是system/libraries/Parse.php的_parse函数会给出foreach参数不对的warning,因为$data默认都是array,但是在你的模板系统中好像有一些时候缺少判断了.
请继续加油,谢谢!
qmi你好,谢谢你发现的问题,我已经做了些修改,你可以再下载试试。
之前已经发到ci中国社区了,呵呵
http://codeigniter.org.cn/forums/thread-10533-1-1.html
很好,收下,
构造函数可以改为用数组接收参数,就可以设置默认的布局文件了。字符串好像不能设置,。也许是我孤落噶问了。
构造函数的参数就是用来指定布局文件啊。