这个是比较好用的模板,比smarty,fasttemplate and phplib template都好用
中文文档案 http://www.cqlc.net/cn_cqlctpl.txt
这里是演示地址:http://www.cqlc.net/web/tml.php
请尊重作者的劳动成果,下面演示的3个文件下载地址:http://www.cqlc.net/cqlctpl.zip
我的主页是:http://www.cqlc.net
1 标签:
(1),变量:{$key} 可以是一般变量,数组和类的成员,但字符不能用 " ,只能是单引号 '
例子: {$value} ,{$a[0]} ,{$this->a['color']} 这样写是错的{$this->["color"]}
(2),循环:{loop:循环语句}其他代码{/loop} 支持for和while循环
例子: {loop:for($i=10;$i<=20;$i++)} test {$i} {/loop} 输出(test)20~30的数
{loop:while($i++<10)} test {$a[$i]} {/loop} 输出(test)20~30的数组
(3),逻辑:{logi:条件语句}其他代码{/logi} 支持if,elseif,else
例子: {logi:if($i==1)} test {$i} {/logi}
{logi:elseif($i==2)} test {$a[$i]} {/logi}
{logi:else} test {$a[$i]} {/logi}
(4),程序:{exec:语句} 程序段,可以是函数,注意字符只能用 ',不能用"
例子 : {exec:$a='hello'.$i} 正确 {exec:$a="hello$i"}错误
{exec:echo $a}
{exec:echo time()}
{exec:$this->outpage(1,2,'index.php')}
(5),{* 模板注释*} 例子:{*这里不会输出的*}
2,模板输出 :
只输出:eval(cqlctpl(模板文件)) 例子:eval(cqlctpl("1.tml"))
套模板:{exec:eval(cqlctpl(模板文件))} 例子:{exec:eval(cqlctpl($templatefile))}
{*这里是在模板中掉用其他模板文件*}
3,函数原代码:
function cqlctpl($file) /*函数作者,cqlc,主页:http://www.cqlc.net,
中文档案 http://www.cqlc.net/cn_cqlctpl.txt */
{
$fp=fopen($file,"r");
$msg="echo\"".str_replace("\"","\\\"",fread($fp,filesize($file)))."\";";
fclose($fp);
$oldstr=array(0=>"/\{loop:([^\}]+)\}(.+)\{\/loop\}/si",
1=>"/\{logi:([^\}]+)\}(.+)\{\/logi\}/si",
2=>"/\{exec:([^\}]+)\}/si",
3=>"/(\{\*[^\*]+\*\})/si"
);
$newstr=array(0=>"\";\\1{echo\"\\2\";}echo\"",
1=>"\";\\1{echo\"\\2\";}echo\"",
2=>"\";\\1;echo\"",
3=>""
);
return preg_replace($oldstr,$newstr,$msg);
}
4,模板举例:
模板文件:1.tml的原代码
<h1> {$title} (1.tml) </h1> {*this is a title*}
{loop:for($i=0;$i<10;$i++)} {*loop start*}
<p> {$body} is {$array[$i]} {*out the msg*}
{logi:if($i>5)} <p> {$i} is biger than 5 {*logic start*}
{/logi} {*login end*}
{/loop} {*loop end*}
{exec:$time=time()} {*exec start and end*}
{exec:echo '<p>time is '.$time} {*exec start and end*}
{exec:eval(cqlctpl('2.tml'))} {*this is include other tml*}
模板文件:2.tml的原代码
<p> <h1>this is other tml(2.tml)</h1>
{ exec:test()}
演示的php脚本
tml.php:
<?
function cqlctpl($file) /*函数作者,cqlc,主页:http://www.cqlc.net,
中文文档案 http://www.cqlc.net/cn_cqlctpl.txt */
{
$fp=fopen($file,"r");
$msg="echo\"".str_replace("\"","\\\"",fread($fp,filesize($file)))."\";";
fclose($fp);
$oldstr=array(0=>"/\{loop:([^\}]+)\}(.+)\{\/loop\}/si",
1=>"/\{logi:([^\}]+)\}(.+)\{\/logi\}/si",
2=>"/\{exec:([^\}]+)\}/si",
3=>"/(\{\*[^\*]+\*\})/si"
);
$newstr=array(0=>"\";\\1{echo\"\\2\";}echo\"",
1=>"\";\\1{echo\"\\2\";}echo\"",
2=>"\";\\1;echo\"",
3=>""
);
return preg_replace($oldstr,$newstr,$msg);
}
function test(){echo"<p>这是第二个模板文件哟 ,欢迎你的测试";} /*定义一个函数*/
$title="这是第一个模板文件";
$body="这里已经开始输出了 ";
for($i=0;$i<10;$i++)$array[$i]="hello world $i";
eval(cqlctpl("1.tml"));
5,模板输出结果,见演示:http://www.cqlc.net/web/tml.php
这是第一个模板文件 (1.tml)
这里已经开始输出了 is hello world 0
这里已经开始输出了 is hello world 1
这里已经开始输出了 is hello world 2
这里已经开始输出了 is hello world 3
这里已经开始输出了 is hello world 4
这里已经开始输出了 is hello world 5
这里已经开始输出了 is hello world 6
6 is biger than 5
这里已经开始输出了 is hello world 7
7 is biger than 5
这里已经开始输出了 is hello world 8
8 is biger than 5
这里已经开始输出了 is hello world 9
9 is biger than 5
time is 1059227729
this is other tml(2.tml)
这是第二个模板文件哟 ,欢迎你的测试
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器