Package genshi :: Package template :: Module directives :: Class ChooseDirective

Class ChooseDirective



object --+    
         |    
 Directive --+
             |
            ChooseDirective

Implementation of the py:choose directive for conditionally selecting one of several body elements to display.

If the py:choose expression is empty the expressions of nested py:when directives are tested for truth. The first true py:when body is output. If no py:when directive is matched then the fallback directive py:otherwise will be used.

>>> from genshi.template import MarkupTemplate
>>> tmpl = MarkupTemplate('''<div xmlns:py="http://genshi.edgewall.org/"
...   py:choose="">
...   <span py:when="0 == 1">0</span>
...   <span py:when="1 == 1">1</span>
...   <span py:otherwise="">2</span>
... </div>''')
>>> print tmpl.generate()
<div>
  <span>1</span>
</div>

If the py:choose directive contains an expression, the nested py:when directives are tested for equality to the py:choose expression:

>>> tmpl = MarkupTemplate('''<div xmlns:py="http://genshi.edgewall.org/"
...   py:choose="2">
...   <span py:when="1">1</span>
...   <span py:when="2">2</span>
... </div>''')
>>> print tmpl.generate()
<div>
  <span>2</span>
</div>

Behavior is undefined if a py:choose block contains content outside a py:when or py:otherwise block. Behavior is also undefined if a py:otherwise occurs before py:when blocks.



Nested Classes

Inherited from Directive: __metaclass__

Instance Methods
 
__call__(self, stream, ctxt, directives)
Apply the directive to the given stream.

Inherited from Directive: __init__, __repr__

Inherited from object: __delattr__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __setattr__, __str__

Class Methods

Inherited from Directive: attach

Class Variables
  ATTRIBUTE = 'test'
  tagname = 'choose'
Properties
  matched
  value

Inherited from Directive: expr

Inherited from object: __class__

Method Details

__call__(self, stream, ctxt, directives)
(Call operator)

 
Apply the directive to the given stream.
Overrides: Directive.__call__
(inherited documentation)