| CARVIEW |
Select Language
HTTP/2 200
server: nginx/1.21.0
date: Fri, 26 Dec 2025 09:55:35 GMT
content-type: text/html
last-modified: Thu, 07 Apr 2016 09:14:53 GMT
etag: W/"5706250d-15dd"
strict-transport-security: max-age=15552000
content-security-policy: upgrade-insecure-requests
x-frame-options: SAMEORIGIN
x-content-type-options: nosniff
content-encoding: gzip
PHPTAL :: Introduction
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">
2013-08-05
</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.
</div>
</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 (using
gettextor your own backend), - ability to replace XPath-like system with PHP expressions,
- nearly no speed loss (templates are compiled, expensive expressions can be replaced with plain PHP).
- compatible with PHP accelerators, which can improve performance even further,
- more than four years of ZPT community support (see zope website),
- extensible and easily hackable code,
- makes good use of PHP5 and SPL.
Some PHPTAL drawbacks:
- a dependency for your application,
- some TAL, METAL, I18N and PHPTAL attributes to understand,
- you'll have to produce clean XHTML (PHPTAL can output HTML5 too).