Package genshi :: Package template :: Module directives :: Class DefDirective

Class DefDirective



object --+    
         |    
 Directive --+
             |
            DefDirective

Implementation of the py:def template directive.

This directive can be used to create "Named Template Functions", which are template snippets that are not actually output during normal processing, but rather can be expanded from expressions in other places in the template.

A named template function can be used just like a normal Python function from template expressions:

>>> from genshi.template import MarkupTemplate
>>> tmpl = MarkupTemplate('''<div xmlns:py="http://genshi.edgewall.org/">
...   <p py:def="echo(greeting, name='world')" class="message">
...     ${greeting}, ${name}!
...   </p>
...   ${echo('Hi', name='you')}
... </div>''')
>>> print tmpl.generate(bar='Bye')
<div>
  <p class="message">
    Hi, you!
  </p>
</div>

If a function does not require parameters, the parenthesis can be omitted in the definition:

>>> tmpl = MarkupTemplate('''<div xmlns:py="http://genshi.edgewall.org/">
...   <p py:def="helloworld" class="message">
...     Hello, world!
...   </p>
...   ${helloworld()}
... </div>''')
>>> print tmpl.generate(bar='Bye')
<div>
  <p class="message">
    Hello, world!
  </p>
</div>


Nested Classes

Inherited from Directive: __metaclass__

Instance Methods
 
__init__(self, args, template, namespaces=None, lineno=-1, offset=-1)
 
__call__(self, stream, ctxt, directives)
Apply the directive to the given stream.
 
__repr__(self)

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

Class Methods

Inherited from Directive: attach

Class Variables
  ATTRIBUTE = 'function'
  tagname = 'def'
Properties
  args
  defaults
  name

Inherited from Directive: expr

Inherited from object: __class__

Method Details

__init__(self, args, template, namespaces=None, lineno=-1, offset=-1)
(Constructor)

 
Overrides: Directive.__init__

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

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

__repr__(self)
(Representation operator)

 
Overrides: Directive.__repr__