Edgewall Software

Changes between Version 21 and Version 22 of MarkupTemplates


Ignore:
Timestamp:
Jul 12, 2006, 3:06:03 AM (18 years ago)
Author:
mgood
Comment:

document the two usage forms of py:choose

Legend:

Unmodified
Added
Removed
Modified
  • MarkupTemplates

    v21 v22  
    1313== Template Context ==
    1414
    15 Rendering a template boils down to applying one or more template files to some input data. This input data is represented as a [wiki:ApiDocs/MarkupTemplate#markup.template:Context Context] object in Markup. A context is basically a stack of dictionaries that manages the namespace scopes during processing: for example, some variable may be able under a given name inside a loop, but not outside of that loop.
     15Rendering a template boils down to applying one or more template files to some input data. This input data is represented as a [wiki:ApiDocs/MarkupTemplate#markup.template:Context Context] object in Markup. A context is basically a stack of dictionaries that manages the namespace scopes during processing: for example, some variable may be available under a given name inside a loop, but not outside of that loop.
    1616
    1717Regardless of its stack behavior during templating processing, a context is usually initialized from a single dictionary:
     
    108108}}}
    109109
    110 The rationale behind the second form is that directives not always map naturally to elements in the template. In such cases, the [wiki:MarkupTemplates#pystrip py:strip] directive can be used to strip off the unwanted element, or the directive can simply be used as an element.
     110The rationale behind the second form is that directives do not always map naturally to elements in the template. In such cases, the [wiki:MarkupTemplates#pystrip py:strip] directive can be used to strip off the unwanted element, or the directive can simply be used as an element.
    111111
    112112=== `py:content` ===
     
    171171<ul>
    172172  <li class="collapse">Bar</li>
     173</ul>
     174}}}
     175
     176Attributes with the value `None` are omitted, so given `foo={'class': None}` in the context for the same template this would produce:
     177
     178{{{
     179#!xml
     180<ul>
     181  <li>Bar</li>
    173182</ul>
    174183}}}
     
    233242=== `py:choose` / `py:when` / `py:otherwise` ===
    234243
    235 This set of directives provides advanced contional processing: one of several alternatives is rendered depending on which of the `py:when` directives evaluates to a truth value.
     244This set of directives provides advanced contional processing for rendering one of several alternatives.  The first matching `py:when` directive will be rendered or, if no `py:when` directive matches, the `py:otherwise` directive will be rendered.
     245
     246If the `py:choose` directive is empty the nested `py:when` directives will be tested for truth:
    236247
    237248{{{
     
    240251  <span py:when="0 == 1">0</span>
    241252  <span py:when="1 == 1">1</span>
     253  <span py:otherwise="">2</span>
     254</div>
     255}}}
     256
     257This would produce the following output:
     258
     259{{{
     260#!xml
     261<div>
     262  <span>1</span>
     263</div>
     264}}}
     265
     266If the `py:choose` directive contains an expression the nested `py:when` directives will be tested for equality to the parent `py:choose` value:
     267
     268{{{
     269#!xml
     270<div xmlns:py="http://markup.edgewall.org/" py:choose="1">
     271  <span py:when="0">0</span>
     272  <span py:when="1">1</span>
    242273  <span py:otherwise="">2</span>
    243274</div>