5.3.8 <al-lookup>/<al-item>

The <al-lookup> tag is used primarily to allow internal application values to be converted to display form. The name attribute is used to uniquely identify the lookup table in the application. Other template HTML accesses the lookup table via the <al-value> tag.

Enclosed by the <al-lookup> tag is a series of <al-item> tags. Each <al-item> tag defines a unique translation of an internal application value to template HTML. The expr attribute defines an expression that is evaluated to generate a lookup table key. When the <al-lookup> tag is executed all of the <al-item> expr expressions are evaluated.

The content enclosed by an <al-item> tag is executed only when the lookup is referenced by an <al-value> tag. If the lookup value does not reference an item then the tag returns from the <al-lookup> any content that was not enclosed by an <al-item> tag.

For example:

>>> import albatross
>>> ctx = albatross.SimpleContext('.')
>>> MILD, MEDIUM, HOT = 0, 1, 2
>>> albatross.Template(ctx, '<magic>',
... '''<al-lookup name="curry">
... <al-item expr="MILD">Mild <al-value expr="curry"></al-item>
... <al-item expr="MEDIUM">Medium <al-value expr="curry"></al-item>
... <al-item expr="HOT">Hot <al-value expr="curry"></al-item>
... </al-lookup>''').to_html(ctx)
>>> ctx.locals.spicy = 2
>>> ctx.locals.curry = 'Vindaloo'
>>> albatross.Template(ctx, '<magic>', '''
... <al-value expr="spicy" lookup="curry" whitespace>
... ''').to_html(ctx)
>>> ctx.flush_content()
Hot Vindaloo

By placing lookup tables in separate template files you can eliminate redundant processing via the run_template_once() execution context method. This method is defined in the AppContext class that is used as a base for all application execution contexts.

As the above example demonstrates, you are able to place arbitrary template HTML inside the lookup items. As the content of the item is only executed when referenced, all expressions are evaluated in the context of the template HTML that references the item.