Writing expressions with TALES
Both METAL & TAL attributes make use of expressions to access values from the object model which was provided to the rendering process. These expressions are written in the Template Attribute Language Expression Syntax: TALES.
TALES expression types
TALES is in fact a collection of expression types, each with its own syntax. In ZPT-Sharp, each expression type is provided by a plugin. The installed plugins determine which expression types are available.
<div tal:define="message string:Welcome!">
Hello
</div>
In the example above, the string expression type is selected by using the prefix
string:
.
One of the installed expression type plugins must be nominated in the application configuration file as the default expression type. Any expressions which are not qualified with a prefix are evaluated using the default expression plugin; typically that is the path expression plugin.
The standard expression types
The following lists a few examples of the 'standard' expression plugins which are installed by the ZPT-Sharp NuGet bundles. At the end of this page, a few other plugins are listed which are not shipped as standard.
Easily access model values
TALES path expressions provide a simplified read-only perspective of a .NET object model.
path:here/CurrentUser/GetShoppingCart/Contents/Count
Path expressions traverse items within an object hierarchy using the forward-slash character. This syntax is familiar to developers and designers alike, as it looks similar to a URL. Expressions may traverse properties, methods (as long as they have no parameters), indexers and readable fields, all using the same notation.
path:user/Nickname | user/FullName | user/Username
Path expressions may also include multiple paths, separated by the pipe symbol. In this case, the first path which is successfully evaluated is returned as the result of the expression.
Path expressions are very powerful; there is more to read about their capabilities on their detailed documentation page.
Negate boolean values
The not expression plugin always prefixes another expression.
not:path:quotes/Hamlet/ToBe
The expression following the not:
prefix is evaluated in isolation and its result is
coerced to either true or false.
Finally, the opposite of that boolean value is returned as the expression result.
The rules for coercing values to true/false are found in the not expression's documentation page.
Write and format strings
The string plugin may be used to provide text values. Its usefulnes comes to light when used with placeholders to inject the results of path expressions into the string.
string:Hello $name; how are you feeling today?
As can be seen from the example, placeholders are denoted with the dollar symbol. More complex paths must be prefixed with a dollar symbol and encapsulated in braces.
string:You have ${user/BooksOnLoan/GetHumanReadableCount | noBooks} on loan
The full documentation page for string expressions goes into further depth about how they are written.
Evaluate C♯ expressions
It is also possible to evaluate native C♯ expressions, using the csharp expression plugin.
csharp:String.Concat(user.Forename.Substring(0, 1), " ", user.Surname)
The example above creates an abbreviated full-name using the first character of the user's forename and their full surname.
Notice that the double-quotation marks in the expression had to be encoded as "
?
Because TALES expressions are written inside HTML/XML attribute values, naked double-quotes are not permitted,
meaning that the encoding is mandatory.
The same is true of greater-than, less-than and ampersand symbols.
There is a lot more to learn about the capabilities and also the limitations of C♯ expressions within ZPT-Sharp. This information is available on C♯ expressions documentation page.
Other expression types
The following expression types not distributed in the standard ZPT-Sharp bundles. They are available via freely installable plugins if desired.
- Python expressions allow you to evaluate native Python expressions, via Iron Python.
- Load expressions instruct ZPT-Sharp to load another document/view and render it using the current model. Particularly useful when combined with TAL content/replace attributes which use the structure modifier.