ZPT Basics (part 1) - The Power Of Three (
Page 2 of 5 )
ZPT actually
consists of three different components, which interact with each other to offer
developers a fair amount of power and flexibility. Here they are:
1. TAL: First up is the Template Attribute Language, also referred to as TAL.
As per the official TAL specification, TAL is "an attribute language used to create
dynamic templates...it allows elements of a document to be replaced, repeated,
or omitted." Put more simply, TAL is a template language which uses special attributes
within HTML tags to perform different actions, like variable interpolation, tag
repetition and content replacement.
2. METAL: Nope, this isn't the music that makes your head hurt and your eardrums
pound. METAL is the Macro Expansion Template Attribute Language, and it is defined
as "an attribute language for structured macro preprocessing". In ZPT, macros
allow developers to create a single snippet of code and reuse it extensively across
the application. The advantage: a change to the macro will be immediately reflected
across the templates it has been used in.
3. TALES: Apart from having a familiar ring to it, this acronym actually stands
for Template Attribute Language Expression Syntax. Officially, TALES "describes
expressions that may be used to supply TAL and METAL with data." These expressions
may be in the form of paths to specific Zope objects, Python code or strings,
including strings containing variables to be substituted, and they form a significant
component of ZPT.
Let's see what all this means with a quick look at some code. {mospagebreak title=What's
In A Name?} Consider the following simple code snippet:
My name is <b tal:content="template/id">template id</b>.
If you were to render this through Zope, in a template with the ID "MyFirstTemplate",
you'd see the following output:
My name is <b>MyFirstTemplate</b>.
What happened here? Roughly translated, the line
<b tal:content="template/id">template id</b>
means "replace the content enclosed within the <b> element with the result
obtained by evaluating the TALES expression [template/id]". When the template
is rendered, this TALES expression will evaluate to the ID of the current page
template, and will appear within the <b> tag.
The TALES expression
template/id
is a path expression pointing to a specific Zope object - in this case, the ID
of the current template - and it provides an easy and intuitive way of accessing
the value of any object within Zope. It need not be restricted to objects alone
either - TALES expressions are equally versatile at evaluating string data or
Python code.
TALES path expressions always begin with a variable name, and drill down hierarchically
to the required object. A TALES path is traversed in a sequential manner until
the final step is reached or until an error occurs. The result of the path expression
is the value of the object found at the end of the path.
The "tal:content" attribute indicates to the Zope parser that *only* the content
within the enclosing element should be replaced with the results of the TALES
expression. TAL comes with a number of such special attributes, each with its
own particular function - I'll be discussing them in detail over the course of
this tutorial.
Note the special "tal:" namespace, which must prefix every TAL attribute, and
which provides developers with an easy way to separate their own custom names
from those defined in the TAL specification.
Usage of the TAL namespace also has one very important additional benefit, closely
related to the developer/designer interaction problem discussed on the previous
page. Most HTML editors will not understand the TAL namespace prefix or attributes,
and will therefore ignore it when rendering a page; this allows designers to view
and make changes to the HTML interface of a page without interfering with its
business logic or requiring the intervention of developers (so long, obviously,
as they don't fiddle with the special TAL statements embedded within the code).