别不多说,直接上代码
template.php(3.55K)
find_var($s); $this->data = explode('<', $s); $this->data[0] = ''; $this->find_dsn(); } //新增 run 方法, function run() { //include "data://," . join('<', $this->data); eval('?>' . join('<', $this->data)); } function find($pattern) { $this->pattern = $pattern; return array_filter($this->data, array($this, 'find_callback')); } private function find_dsn() { foreach($this->find("#\bdsn\b#i") as $k=>$v) { $t = $this->find_tag($tag = strtok($v, ' '), $k); end($t); $dsn[] = array( $k, key($t) ); } if($this->drillmode) { foreach($this->find("#\bdrill\b#i") as $k=>$t) { foreach($dsn as $i=>$v) if($k < $v[1] && $k > $v[0]) $t = $i; $drill[] = $dsn[$t]; unset($dsn[$t]); } } foreach($dsn as $v) { list($start, $end) = $v; preg_match('/\bdsn\s*=\s*([^\s>]+)/i', $this->data[$start], $reg); $this->data[$start] = str_replace(' '.$reg[0], '', $this->data[$start]); $m = explode(',', trim($reg[1], '\'"')) + array(0, 0, ''); $code_start = "?php if(isset(\$_var))\$_st[]=\$_var;foreach((isset(\$_var['$m[0]'])?\$_var['$m[0]']:\$this->$m[0]('$m[1]','$m[2]')) as \$_key=>\$_var){?>"; $code_end = "?php }\$_var=array_pop(\$_st);?>"; switch($m[1]) { case 0: $t = explode('>', $this->data[$start]); $t[1] = "<$code_start" . $t[1]; $this->data[$start] = join('>', $t); $this->data[$end] = "$code_end<" . $this->data[$end]; break; case 1: $this->data[$end] .= "<$code_end"; $this->data[$start] = "$code_start<" . $this->data[$start]; break; default: $n = round(100/$m[1]); $this->data[$end] .= "<$code_end"; $this->data[$start] = "$code_start补充一下
模板中只有两种控制元素:
1、模板变量 用花括号括起的变量名,如 {var}。提供数据的函数应提供对应的数据,如没有提供则自动为空
2、数据源声明 位于 HTML 标记中的 dsn 属性,比如
{t}
其中 foo 表示提供数据的函数,1 表示充填数据时,循环是包含自身的 比如返回的数据是 array(array('t'=>1,array('t'=>2))
则实际生成的 html 是
1
2
当第二个参数缺省时,同样的数据只产生
12
3、数据源函数约定返回的是二维数组:第一维是下标数组,第二维是关联数组。请参见测试例
写程序很简单,写文档就要了我的命了
相关问题解答见 #39
一个扩展的用法见 #61
回复讨论(解决方案)
测试例
include 'template.php';function body() { return array( array( 'title' => '布局', 'advertising' => '一贯喜欢用表格布局,这种 DIV+CSS 方式很让人头疼的', 'menu' => array( array('text' => '首页' ), array('text' => '博客' ), array('text' => '设计' ), array('text' => '相册' ), array('text' => '论坛' ), array('text' => '关于' ), ), 'footer' => '为什么表现不一样呢,IE就是对齐的', ), );}function img() { $ar['url'] = 'http://justinyoung.cnblogs.com/images/cnblogs_com/justinyoung/2007/tb_m.PNG'; return array($ar);}function tree() {$data = array( array('ID'=>1, 'PARENT'=>0, 'NAME'=>'祖父'), array('ID'=>2, 'PARENT'=>1, 'NAME'=>'父亲'), array('ID'=>3, 'PARENT'=>1, 'NAME'=>'叔伯'), array('ID'=>4, 'PARENT'=>2, 'NAME'=>'自己'), array('ID'=>5, 'PARENT'=>4, 'NAME'=>'儿子'),);$t = find_child($data, 'ID', 'PARENT');//print_r($t);return array($t[1]);}function box() { return array( array('t'=>'--1--', 'child' => array(array('t'=>2),array('t'=>2),array('t'=>2),)), array('t'=>'--2--', 'child' => array(array('t'=>2),array('t'=>2),array('t'=>2),)), array('t'=>'--3--', 'child' => array(array('t'=>2),array('t'=>2),array('t'=>2),)), array('t'=>'--4--', 'child' => array(array('t'=>2),array('t'=>2),array('t'=>2),)), array('t'=>'--5--', 'child' => array(array('t'=>2),array('t'=>2),array('t'=>2),)), array('t'=>'--6--', 'child' => array(array('t'=>2),array('t'=>2),array('t'=>2),)), );}$p = new template('布局.txt');$p->run(); //使用也做相应调整 模板文件(布局.txt)
{title} {advertising}
- {NAME}{drill}
{t}{t}![]()
ci1699的AMySQL赶超phpmyadmin;
版主这是要赶超smarty啊。
啥也不说了,弄下来试试先!
能不能加点注释啊,猛一看不太明白,仔细一看还不如猛一看
正需要模板,哈哈,太感谢了
先做个记号,等有空慢慢研究。
能不能加点注释啊,猛一看不太明白,仔细一看还不如猛一看
正需要模板,哈哈,太感谢了 你都弄下来,尝试运行一下。就应该明白了
xuzuning ?? 意外 盗号了? 很少有啊 标记 晚上看看
xuzuning ?? 意外 盗号了? 很少有啊 标记 晚上看看 你上次不是要我晾我的模板引擎吗?当时包在工具箱中,这次剥离出来了
确实牛B了。强人那。
围观教科书...过两天仔细品读.
Fatal error: Call to undefined function find_child() in E:\test.local\test.php on line 51
出现了这个错误。是怎么回事啊?
find_child() 函数是我上次发的 两个不用递归的树形数组构造函数 中的一个
不错啊支持了呵呵
不理解了:
Warning: include() [function.include]: data:// wrapper is disabled in the server configuration by allow_url_include=0 in \path\template_csdn\template_class.php on line 13Warning: include(data://, body('0','')) as $_key=>$_var){?>
