This is a simple reference guide to the TAL and TALES languages. Formal language specifications are hosted by Zope online. Axiom TAL differs slightly in the noted areas.
Contents |
TAL Commands
TAL consists of eight different commands. There is a clear order of operations by which they will be evaluated. Commands are attributes on HTML or XML tags.
Example:<div tal:content="article">Article goes here</div>
The order of operations is as follows:
- define
- condition
- repeat
- repeat-children
- content
- replace
- attributes
- omit-tag
tal:define
tal:define="[local | axdevdoc:global] name expression [axdevdoc:; define-expression...]
Sets the value of "name" to "expression". By default the name will be applicable in the "local" scope, which consists of this tag, and all other tags nested inside this tag. If the "global" keyword is used then this name will keep its value for the rest of the document.
Example:<div tal:define="global title book/theTitle; local chapterTitle book/chapter/theTitle">
tal:condition
tal:condition="expression"
If the expression evaluates to true then this tag and all its children will be output. If the expression evaluates to false then this tag and all its children will not be included in the output.
Example:<h1 tal:condition="user/firstLogin">Welcome to this page!</h1>
tal:repeat
tal:repeat="name expression"
Evaluates "expression", and if it is a sequence, repeats this tag and all children once for each item in the sequence. The "name" will be set to the value of the item in the current iteration, and is also the name of the repeat variable. The repeat variable is accessible using the TAL path: repeat/name and has the following properties:
- index - Iteration number starting from zero
- number - Iteration number starting from one
- even - True if this is an even iteration
- odd - True if this is an odd iteration
- start - True if this is the first item in the sequence
- end - True if this is the last item in the sequence. For iterators this is never true
- length - The length of the sequence. For iterators this is maxint as the length of an iterator is unknown
- letter - The lower case letter for this iteration, starting at "a"
- Letter - Upper case version of letter
- roman - Iteration number in Roman numerals, starting at i
- Roman - Upper case version of roman
Example: <table> <tr tal:repeat="fruit basket"> <td tal:content="repeat/fruit/number"></td> <td tal:content="fruit/name"></td> </tr> </table>
tal:repeat-children
tal:repeat-children="name expression"
Behaves identically to tal:repeat, except that only the child nodes are repeated.
tal:content
tal:content="expression"
Replaces the contents of the tag with the value of "expression". Axiom differs from Zope TAL in that there is no need for the "structure" prefix. DOM Documents and DocumentFragments are imported as nodes, and all other types are converted into strings and quoted for XML representation.
tal:attributes
tal:attributes="name expression[axdevdoc:;attributes-expression]"
Evaluates each "expression" and replaces the tag's attribute "name". If the expression evaluates to nothing then the attribute is removed from the tag. If the "expression" requires a semi-colon then it must be escaped by using ";;".
Example:<a tal:attributes="href user/homepage;title user/fullname">Your Homepage</a>
Note: If you use a javascript reserved keyword for the name, you must enclose it in quotes. E.g.:
Example:<div tal:attributes="'class' user/homepage;id user/fullname">Your Homepage</a>
tal:omit-tag
tal:omit-tag="expression"
Removes the tag (leaving the tags content) if the expression evaluates to true. If expression is empty then it is taken as true.
Example:<p><b tal:omit-tag="not:user/firstVisit">Welcome</b> to this page!</h1>
tal:text
tal:text="escape-character"
Replaces all strings matching escape-character{javascript-expression} in the top-level text nodes beneath this element.
Example:<div tal:text="$">Logged in as: ${session.user.username}</div>
TALES Expressions
The expressions used in TAL are called TALES expressions. The simplest TALES expression is a path which references a value, e.g. page/body references the body property of the page object.
path
[path:]string[| axdevdoc:Expression]
A path, optionally starting with the modifier 'path:', references a property of an object. The '/' delimiter is used to end the name of an object and the start of the property name. Properties themselves may be objects that in turn have properties. The '|' ("or") character is used to find an alternative value to a path if the first path evaluates to null or does not exist.
Example:<p tal:content="book/chapter/title | string:Untitled"></p>
There are several built in paths that can be used in paths:
- repeat - access the current repeat variable (see tal:repeat)
- attrs - a dictionary of original attributes of the current tag
exists
exists:path
Returns true if the path exists, false otherwise. This is particularly useful for removing tags from output when the tags will have no content.
Example:<p tal:omit-tag="not:exists:book/chapter/title" tal:content="book/chapter/title"></p>
References
Zope documentation on the [TAL directives].
not
not:tales-path
Returns the inverse of the tales-path. If the path returns true, not:path will return false.
Example:<p tal:condition="not: user/firstLogin">Welcome to the site!</p>
string
string:text
Evaluates to a literal string with value text while substituting variables with the form ${pathName} and $pathName
Example:<b tal:content="string:Welcome ${user/name}!"></b>
