使用ZTemplate
php文件示例:
$title = 'welcome';$users = array('alen', 'blen', 'calon');include template('main');
header.html模版文件示例:
<html><head> <meta http-equiv=content-type content="text/html; charset=UTF-8"> <title>{$title}</title></head><body>
main.html模版文件示例:
<!--{include header}--><!--{loop $users $index $one}--><li>${$index+1}-{$one}</li><!--{/loop}--><!--{include footer}-->
{}作为模版引擎的开始和结束标记,如果{}内部使用php运算符、函数调用等复杂语句,则需在{前加$,正确输出:
1-alen2-blen3-calon
footer.html模版文件示例:
</body></html>
否则,标记里面的内容不会被解析,输出如下:
{$index+1}-alen{$index+1}-blen{$index+1}-calon
模版中可以使用简单的php控制结构,下面的示例中包含了循环结构的使用,另外,还支持if,if/else,if/elseif/else等条件判断,本模版本着“大道至简”的设计思想,简单实用就已经足够了,所以,也仅仅支持这两种控制结构。
<!--{loop $users $index $one}--><!--{if $index==0}--><li>${md5($one)}-{$one}</li><!--{elseif $index==1}--><li>${md5($one)}-{$one}</li><!--{else}--><li>${md5($one)}-{$one}</li><!--{/if}--><!--{/loop}-->
输出:
f3f7eb1421dcfed9a2614712ec608f1b-alen37670a9c2cc598bcc271612af0617c3c-blen82a8ea39dd2700b9c6dd207d512bb62a-calon