一个很好的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'.

<?xml version="1.0"?> <html> <head> <title tal:content="title"> Place for the page title </title> </head> <body> <h1 tal:content="title">sample title</h1> <table> <thead> <tr> <th>Name</th> <th>Phone</th> </tr> </thead> <tbody> <tr tal:repeat="person people"> <td tal:content="person/name">person's name</td> <td tal:content="person/phone">person's phone</td> </tr> <tr tal:replace=""> <td>sample name</td> <td>sample phone</td> </tr> <tr tal:replace=""> <td>sample name</td> <td>sample phone</td> </tr> </tbody> </table> </body> </html>

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

<?php require_once 'PHPTAL.php'; // create a new template object $template = new PHPTAL('my_template_file.xhtml'); // the Person class class Person { public $name; public $phone; function Person($name, $phone) { $this->name = $name; $this->phone = $phone; } } // let's create an array of objects for test purpose $people = array(); $people[] = new Person("foo", "01-344-121-021"); $people[] = new Person("bar", "05-999-165-541"); $people[] = new Person("baz", "01-389-321-024"); $people[] = new Person("quz", "05-321-378-654"); // put some data into the template context $template->title = 'The title value'; $template->people = $people; // execute the template try { echo $template->execute(); } catch (Exception $e){ echo $e; } ?>

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


<?xml version="1.0"?>
<html>
  <head>
    <title>The title value</title>
  </head>
  <body>
    <h1>The title value</h1>
    <table>
      <thead>
        <tr>
          <th>Name</th>
          <th>Phone</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>foo</td>
          <td>01-344-121-021</td>
        </tr><tr> <td>bar</td>
          <td>05-999-165-541</td>
        </tr><tr> <td>baz</td>
          <td>01-389-321-024</td>
        </tr><tr> <td>quz</td>
          <td>05-321-378-654</td>
        </tr>
      </tbody>
    </table>
  </body>
</html> 

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.

你可能感兴趣的:(PHP,exception,library,模板引擎,variables,html解析器)