5.2.2.1 alias="..." attribute

This attribute is ignored is any of the following attributes are present; prevpage, nextpage (5.2.2.9), treefold, treeselect, or treeellipsis (5.2.2.10).

The value of the alias attribute is passed to the make_alias() method of the execution context. The return value is then used as the generated name (5.2.2.5) attribute.

The execution context make_alias() method splits the alias attribute at the last '.' and resolves the left hand side to an object reference. The albatross_alias() method is then called on that object and the result is combined with the '.' and the right hand side of the of the alias attribute to produce the generated name attribute. The resolved object is entered in the the local namespace and the session using the name returned by the albatross_alias() method.

The template samples/tree/tree1.html contains an example of this method for generating a name attribute.

<al-tree iter="n" expr="tree">
 <al-for iter="c" expr="range(n.depth())">
  <al-value expr="n.line(c.value())" lookup="indent">
 </al-for>
 -<al-input type="checkbox" alias="n.value().selected">
  <al-value expr="n.value().name" whitespace="newline">
</al-tree>

Note that each node in the tree has a checkbox that controls whether or not the node is selected. When processing the alias attribute the toolkit isolates the left hand side (n.value()) which happens to be the current tree node of TreeIterator n. To generate the alias the albatross_alias() method of the current node is called. In samples/tree/tree1.py the implementation of that method looks like:

class Node:
    def albatross_alias(self):
        return 'node%d' % self.node_num

When the template is executed a unique name is generated for each checkbox. The exact HTML produced by the above fragment from the sample looks like this:

-<input type="checkbox" name="node12.selected" value="on">a
 |-<input type="checkbox" name="node2.selected" value="on">a
 | |-<input type="checkbox" name="node0.selected" value="on">a
 | \-<input type="checkbox" name="node1.selected" value="on">b
 \-<input type="checkbox" name="node11.selected" value="on">b
   |-<input type="checkbox" name="node6.selected" value="on">a
   | \-<input type="checkbox" name="node5.selected" value="on">a
   |   |-<input type="checkbox" name="node3.selected" value="on">a
   |   \-<input type="checkbox" name="node4.selected" value="on">b
   |-<input type="checkbox" name="node7.selected" value="on">b
   \-<input type="checkbox" name="node10.selected" value="on">c
     |-<input type="checkbox" name="node8.selected" value="on">a
     \-<input type="checkbox" name="node9.selected" value="on">b

The alias handling uses the fact that all Python objects are stored by reference. It obtains a reference to an existing object by resolving an expression and stores that reference under a new name. Since both the original expression and the new name are the same reference, the toolkit can modify the object referenced by the original expression by using the new name.

Looking further into the samples/tree/tree1.py code you will note that the tree being iterated is generated once and placed into the session. This ensures that the alias names generated always contain references to the nodes in the tree. If the tree was not entered into the session but was generated from scratch every request, the nodes referenced in the alias names would not be the same nodes as those in the tree so all input would be lost.