Eliminate Path Modifiers¶
The path modifiers of TALES 1.2 were introduced in an attempt to provide simple spellings for two common capabilities:
- Choose between a path value and the default text, based on a boolean test.
- Deal well with invalid paths.
Several people (including the implementor) have found path modifiers to be confusing and inadequate. In a three hour meeting between Jim Fulton, Guido van Rossum, and Evan Simpson, the following proposed changes were worked out.
Features That Have Been Implemented¶
Add the alternation operator
|to path expressions.
A path expression can now contain multiple path separated by
|. The value of the expression is the value of the first path that exists (proceeding from left to right). Spaces surrounding a
|are not significant. If none of the paths exist, then the result is the same as if only the final path appeared in the expression.
<title tal:replace="request/title | here/title">Untitled</title>
Add new builtin variable
defaultthat can be used to keep default text.
defaultvariable, like the
nothingvariable, contains a special value that affects TAL statements. Where
attributesstatements to delete their targets,
defaultcauses them to leave the existing target text alone. It is most useful when combined with the alternation operator.
existsfunctions to Python expressions.
We decided to relegate more complex logic that tests for path existence to Python expressions, and make Python expressions more capable of dealing with paths.
pathfunction takes a string, evaluates it in exactly the same way that the path expression type would, and returns the result. If the string doesn’t resolve to an existing path, the result is an error object.
existsfunction takes a string, tries to treat it as a path, and returns true if it is a valid path, false if it isn’t.
<div tal:condition="python:exists('a/b/c')" tal:replace="a/b/c/d" /> <div tal:replace="python:path('a/b') + 1" />
Features That Are Not Implemented¶
The following items have not yet been implemented:
Eliminate path modifiers.
nocallby adding a trailing slash to a path.
This means that
modules/string/join/will get you the
joinfunction, instead of an error about calling it with too few arguments.
Provide an alternation operator
||which works like
|, except that it treats the preceding path as non-existent if its value is false. Whether this use-case is significant enough to merit its own syntax is unknown.
As alternatives to these, we could keep only the path modifiers
if, we can write “(if) a | b” instead of