plasTeX 3.0 — A Python Framework for Processing LaTeX Documents

D.1 Template Attribute Language Expression Syntax (TALES)

The Template Attribute Language Expression Syntax (TALES) is used by the attribute language described in the next section. The TALES syntax is used to evaluate expressions based on objects in the template namespace. The results of these expressions can be used to define variables, produce output, or be used as booleans. There are also several operators used to modify the behavior or interpretation of an expression. The expressions and their modifiers are described below.

path: operator

A “path” is the most basic form on an expression in ZPT. The basic form is shown below.

[path:]string [ | TALES expression ]

The path:  operator is actually optional on all paths. Leaving it off makes no difference. The “string” in the above syntax is a ’/’ delimited string of names. Each name refers to a property of the previous name in the string. Properties can include attributes, methods, or keys in a dictionary. These properties can in turn have properties of their own. Some examples of paths are shown below.

# Access the parentNode attribute of chapter, then get its title
chapter/parentNode/title

# Get the key named 'foo' from the dictionary bar
bar/foo

# Call the title method on the string in the variable booktitle
booktitle/title

It is possible to specify multiple paths separated by a pipe (|). These paths are evaluated from left to right. The first one to return a non-None value is used.

# Look for the title on the current chapter node as well as its parents
chapter/title | chapter/parentNode/title | chapter/parentNode/parentNode/title

# Look for the value of the option otherwise get its default value
myoptions/encoding | myoptions/defaultencoding

There are a few keywords that can be used in place of a path in a TALES expression as well.

Name

Purpose

nothing 

same as None  in Python

default 

keeps whatever the existing value of the element or attribute is

options 

dictionary of values passed in to the template when instatiated

repeat 

the repeat variable (see ??)

attrs 

dictonary of the original attributes of the element

CONTEXTS 

dictionary containing all of the above

exists: operator

This operator returns true if the path exists. If the path does not exist, the operator returns false. The syntax is as follows.

exists:path

The “path” in the code above is a path as described in section ??. This operator is commonly combined with the not: operator.

nocall: operator

By default, if a property that is retrieved is callable, it will be called automatically. Using the nocall: operator, prevents this execution from happening. The syntax is shown below.

nocall:path

not: operator

The not: operator simply negates the boolean result of the path. If the path is a boolean true, the not: operator will return false, and vice versa. The syntax is shown below.

not:path

string: operator

The string: operator allows you to combine literal strings and paths into one string. Paths are inserted into the literal string using a syntax much like that of Python Templates: $path or ${path}. The general syntax is:

string:text

Here are some examples of using the string: operator.

string:Next - ${section/links/next}
string:($pagenumber)
string:[${figure/number}] ${figure/caption}

python: operator

The python: operator allows you to evaluate a Python expression. The syntax is as follows.

python:python-code

The “python-code” in the expression above can include any of the Python built-in functions and operators as well as four new functions that correspond to the TALES operators: path, string, exists, and nocall. Each of these functions takes a string containing the path to be evaluated (e.g. path(’foo/bar’), exists(’chapter/title’), etc.).

When using Python operators, you must escape any characters that would not be legal in an XML/HTML document (i.e. <>&). For example, to write an expression to test if a number was less than or greater than two numbers, you would need to do something like the following example.

# See if the figure number is less than 2 or greater than 4
python: path('figure/number') &lt; 2 or path('figure/number') &gt; 4

stripped: operator

The stripped: operator only exists in the SimpleTAL distribution provided by plasTeX. It evaluates the given path and removes any markup from that path. Essentially, it is a way to get a plain text representation of the path. The syntax is as follows.

stripped:path