The Template Attribute Language (TAL) is a templating language used to generate dynamic HTML and XML pages. Its main goal is to simplify the collaboration between programmers and designers. This is achieved by embedding TAL statements inside valid HTML (or XML) tags which can then be worked on using common design tools.
TAL was created for Zope but is used in other Python-based projects as well.
Contents |
TAL templates are often made from result pages with the variable content being replaced by special attributes; e.g.,
<h1 tal:content="string:Some altogether different headline"> The headline visible to the designer </h1>
would be treated by a common HTML design tool as
<h1> The headline visible to the designer </h1>
while the application server, evaluating the tal:content attribute, would yield
<h1> Some altogether different headline </h1>
Of course, string literals are not the most common nor the most useful use case; syntax of the possible attribute values are properties of the TAL Expression Syntax (TALES) language. In Python-based implementations, it is possible to use Python expressions.
Using METAL (Macro Expansion TAL) it is possible to re-use code from other templates.
When generating XML documents, it is important to specify the XML namespace xmlns:tal="http://xml.zope.org/namespaces/tal"
The following attributes are used, normally prefixed by "tal:
":
tal:attributes="name name; id name"
the name and id attributes of an input field could be set to the value of the variable "name
")If a tag has more than one TAL attributes, they are evaluated in the above (fairly logical) order.
In cases when no tag is present which lends itself to take the attributes, special TAL tags can be used, making the "tal:
" prefix optional. e.g.:
<tal:if condition="context/itemlist"> ... </tal:if>
would cause the code inside the tal:if
tags to be used whenever the context (whatever the application server defines the context to be, e.g. an object) contains variable "itemlist
" with a true value, e.g. a list containing at least one element. The identifier following the colon is arbitrary; it simply needs to be there, and to be the same for the opening and closing tag.
The Macro Expansion Template Attribute Language complements TAL, providing macros which allow the reuse of code across template files. Both were created for Zope but are used in other Python projects as well.
METAL complements TAL with the ability to reuse code. It allows the developer to define and use macros, which in turn may have slots; when using a macro, variational content can be specified for a slot.
When Generating XML documents, the XML namespace must be specified (xmlns:metal="http://xml.zope.org/namespaces/metal"
).
The following attributes are recognised, normally requiring a „metal:
“ prefix:
Normally, just one of those is used at a time.
In cases when no tag is present which lends itself to take the attributes, and in special cases when more than one METAL attribute is needed, special METAL tags can be used, making the „metal:“ prefix optional. E. g. (sketched with Roundup in mind):
<html metal:define-macro="icing"> ... <metal:myslot define-slot="optional-form"> ... </html> <html metal:use-macro="templates/page/macros/icing"> <form metal:fill-slot="optional-form" action="." tal:attributes="action context/designator"> ... </form> </html>
TAL/TALES/METAL are used by the following projects:
Besides the original Zope implementation, there are (not exhaustive):