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, directives, ctxt, **vars)
Apply the directive to the given stream.

Inherited from Directive: __init__, __repr__

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

Class Methods
 
attach(cls, template, stream, value, namespaces, pos)
Called after the template stream has been completely parsed.
Class Variables
  tagname = 'choose'
Properties
  matched
  value

Inherited from Directive: expr

Inherited from object: __class__

Method Details

attach(cls, template, stream, value, namespaces, pos)
Class Method

 

Called after the template stream has been completely parsed.

This class method should return a (directive, stream) tuple. If directive is not None, it should be an instance of the Directive class, and gets added to the list of directives applied to the substream at runtime. stream is an event stream that replaces the original stream associated with the directive.

Parameters:
  • template - the Template object
  • stream - the event stream associated with the directive
  • value - the argument value for the directive; if the directive was specified as an element, this will be an Attrs instance with all specified attributes, otherwise it will be a unicode object with just the attribute value
  • namespaces - a mapping of namespace URIs to prefixes
  • pos - a (filename, lineno, offset) tuple describing the location where the directive was found in the source
Overrides: Directive.attach
(inherited documentation)

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

 
Apply the directive to the given stream.
Parameters:
  • stream - the event stream
  • directives - a list of the remaining directives that should process the stream
  • ctxt - the context data
  • vars - additional variables that should be made available when Python code is executed
Overrides: Directive.__call__
(inherited documentation)