Introduction
PHPTAL is a PHP implementation of ZPT work. To be short, PHPTAL is a XML/XHTML template library for PHP.
While most web developpers continue to use ASP/JSP/PHP tags as the core language of their templates, the Zope community came with a refreshing idea named TAL. The idea was to move presentation actions inside XHTML attributes instead of using plain tags or elements.
Let's start with a simple PHP exemple (usually reproduceable in ASP/JSP):
<?php foreach ($values as $value): ?>
<div class="item">
<div class="title">
<?php if ($value->hasDate()): ?><?=$value->getDate()?><?php endif; ?>
<a href="<?= $value->getUrl() ?>"><?=
htmlentities($value->getTitle())
?></a>
</div>
<div class="content">
<?= htmlentities($value->getContent()) ?>
</div>
</div>
<?php endforeach; >
Let's have a look at the TAL way:
<div class="item" tal:repeat="value values">
<div class="title">
<span tal:condition="value/hasDate" tal:replace="value/getDate"/>
<a tal:attributes="href value/getUrl" tal:content="value/getTitle"/>
</div>
<div id="content" tal:content="value/getContent"/>
</div>
Now it's up to you to choose which version you prefer. Of course those
tal:condition, tal:replace and
tal:repeat may looks strange when begining with TAL.
Just for fun let's modify this example:
<div class="item" tal:repeat="value values">
<div class="title">
<span tal:condition="value/hasDate" tal:replace="value/getDate">
2005-01-28
</span>
<a href="sample.html"
tal:attributes="href value/getUrl"
tal:content="value/getTitle">
My item title
</a>
</div>
<div class="content" tal:content="value/getContent">
This is a sample content which is replaced by the
real content when the template is run with real
data.
</span>
</div>
More in the manual.
Some PHPTAL advantages and features:
- enforces the separation between logic and presentation (the holy grail),
- warns you if you forget to close an HTML tag or have syntax errors in your template,
- no more
htmlentities(), - quite clean and readable templates,
- ability to insert sample text inside template to preview template result without PHP backend,
- integrates quite well with WYSIWYG HTML editors,
- data abstraction using XPath-like system,
- cool HTML macro system,
- integrated internationalization system,
- ability to replace XPath-like system with PHP expressions,
- nearly no speed loss (templates are compiled, expensive expressions can be replaced with plain PHP).
- more than four years of ZPT community support (see zope websize),
- extensible and easily hackable code.
Some PHPTAL drawbacks:
- a dependency for your application,
- some TAL, METAL, I18N and PHPTAL attributes to understand,
- you'll have to produce clean XHTML.