Template Attribute Language Expression Syntax
The Template Attribute Language Expression Syntax ( TALES ) describes the syntax for evaluating the expressions used by the Template Attribute Language (TAL) and Macro Expansion Template Attribute Language (METAL) for attribute values. The option of using a syntax other than TALES for this is expressly provided; as a rule, however, TALES is not replaced, but expanded.
The web application server Zope extends the TALES specification within the scope of the Zope page templates .
overview
TALES provides several methods for formulating expressions that can be used in TAL and METAL attributes and are distinguished by an (optional) prefix:
Expressions
- Path expression
- Prefix
path:
(optional, and therefore almost always left out): for access to structured variables, file system objects, macros (METAL) etc. - String expression
- Prefix
string:
; allows path expressions in${...}
- Logical negation
- Prefix
not:
; evaluates the rest of the expression and returns its logical negation - Python expression
- Prefix
python:
; if implemented, returns the value of the following Python expression. This is usually only used when path expressions are insufficient, e.g. B. when a function has to be called with certain arguments. - Suppression of quoting
- The preceding keyword
structure
( without a colon) suppresses the otherwise automatically applied HTML quoting, for example to generate a complete HTML element.
Built-in names
- nothing
- a "non-value" that cannot be a character string , e.g. B. None ( Python programming language), void , Nil , or NULL .
- options
- the (named) arguments passed to the template
- repeat
- to access loop variables such as index , number , etc.
These names are usually supplemented with more, see Zope Page Templates .
Examples
Simple path expression for accessing a macro
<metal:form use-macro="location/page/MACROS/myform"/>
For example, here is location
the directory that contains a template page
file in which the macro was myform
defined. In Zope Page Templates , instead of location
e.g. B. here
(as a starting point for the acquisition to search for page
) or container
stand.
Loop with path, python and string expressions
<tr tal:repeat="item python:({'key': 'eins'}, {'key': 'zwei'})"> <td tal:content="repeat/item/number">99</td> <td tal:content="string:Zeile ${item/key}">Zeile 99</td> </tr>
Output:
<tr> <td>1</td> <td>Zeile eins</td> </tr> <tr> <td>2</td> <td>Zeile zwei</td> </tr>
Explanations:
- the
tal:repeat
attribute creates a variableitem
and, using the Python expression, a sequence of dictionaries that is used to create multiple rows of HTML tables - the special loop variable (z. B. number) for
item
are special, required by TALES standard variablerepeat
provided - Path expressions can also be used within string expressions
Logical negation and TAL elements
<div tal:define="view_ok context/is_view_ok"> <tal:if condition="view_ok"> ... </tal:if> <tal:else condition="not:view_ok"> ... </tal:else> </div>
is_view_ok could be a method that is called from the context and checks the read authorization of the user . In the “Else branch” (which is syntactically nothing else than the “If branch”; if
and else
there are freely chosen names that should even have been the same) the logical condition of the If branch is reversed so that only one of the two is executed becomes.
See also
Web links
- TALES specification, version 1.3
- Loop variables ( Memento from September 9, 2012 in the Internet Archive )
- TALES Overview (in the Zope Book)
- TALES page in the Zope 3 Wiki ( Memento from September 14, 2011 in the Internet Archive )