TemplessDirectives
There are only 5 directives in Templess, all of which are defined as attributes on an XML node (see the examples below):
- content
When an XML node has a templess:content attribute, a lookup will be done in the context (see the examples below) using the value of the attribute as key. If the key can not be found, a KeyError is raised. If the value found is a string, it will be used as text content for the node. If the value found is a 'lazyrenderer' instance as produced by template.render() it will be used as new content for the element (iow, you can use template.render() to generate snippets of XML to be placed into other trees). If the value is a list node, the element's contents will be repeated for each element in the list, interpolating the current item into the elements' contents (see list interpolation below).
- replace
Same as content, but instead of 'filling' the node, the node gets replaced by the result of the interpolation.
- attr
The value of an optional templess:attr on an XML node will be split on ; to form pairs (strings), which are each split on space (' ') to form key, value pairs (so 'foo bar;baz qux' will result in two pairs, ('foo', 'bar'), ('baz', 'qux') of which each first item is the key and the second the value). For each of those pairs an attribute will be added to the XML node, with the key as name, and as value the result of a lookup in the context.
- cond
If the value of the templess:cond condition on an XML node resolves to false, the node is removed entirely from the document.
- not
The reverse of 'cond', this makes the node get rendered if the value is false only.
List interpolation
If the interpolation value found for a templess:content or templess:replace attribute is of a string or a node type, the value will just be added to a node. If the item's value is a list type, the node will be copied and the interpolation will be executed repetetively for each item in the list, using the node as the root and the current value as the context. For each list item it will respond in the following manner:
- If the item value is a string type, the value will be used as the current element's text attribute.
- If the item value is a node type, it will be attached to the node's children.
- If the item value is a dict type, the content interpolation will recurse with the exact same behaviour as it has with the full template, using the current element as root and the current context value as context (note that the root context is not available anymore).
When the nodes are all copied, they are attached to the tree if the directive used was templess:content, and only their contents are attached to the tree if the directive was templess:replace.
