一个很好的PHP模板引擎(Template engine)——PHPTAL

这是《PHP in Action》书中着重提到的一个作者最喜欢的PHP引擎,他是基于模板属性语言(template attribute language)的引擎,他是用XML属性而不是像smaty这种用特殊的标记(如{})来写模板的。

smaty的写法:

<td>{$username}</td>

而PHPTAL的写法:

<td tal:content="username">Dummy user name</td>

这样最大的好处是用WYSIWYG的html编辑器可以很友好的看到页面效果,而不会有很多的{$username}标记。因为html解析器遇到不能解析的属性tal:content="username"的时候会自动忽略的。更好的是,你可以在标记间加入虚拟的显示信息,在模板载入的时候他会被自动替换掉,这样designer和programmer可以很好的分工,code和presentation可以分离!

下面是一个使用PHPTAL的例子:

Installation


PHPTAL is released as a PEAR package (see pear.php.net). You can download the PHPTAL library on the PHPTAL website: phptal.org.

You can install it using the PEAR utility:

pear install http://phptal.org/latest.tar.gz






Once installed, you can upgrade PHPTAL easily on each PHPTAL update using PEAR:

pear upgrade http://phptal.org/latest.tar.gz






If you do not use PEAR or do not have it installed on your system, you can still install PHPTAL by unzipping the downloaded archive.

tar zxvf PHPTAL-X.X.X.tar.gz



cp -r PHPTAL-X.X.X/PHPTAL* /path/to/your/lib/folder






This will install the PHPTAL.php file and the associated PHPTAL folder in /path/to/your/lib/folder.

 

First example


To get a first impression of PHPTAL usage, a simple example is better than many words.

Your template is a valid XML/HTML document (with a root element). Here's a file named 'my_template_file.xhtml'.

In PHP, you just have to include the PHPTAL library, and maybe configure a few variables to customize the template system.

If you execute the PHP script, you will obtain something similar to what follows.


 

PHPTAL doesn't care much about line breaks and indentation in files it reads and generates. If you want source code of generated HTML files to be pretty (with line breaks and perfect indentation), then you might need to postprocess it with HTML Tidy.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值