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>
|
__init__(self,
args,
template,
namespaces=None,
lineno=-1,
offset=-1) |
|
|
|
__call__(self,
stream,
ctxt,
directives)
Apply the directive to the given stream. |
|
|
|
|
Inherited from object :
__delattr__ ,
__getattribute__ ,
__hash__ ,
__new__ ,
__reduce__ ,
__reduce_ex__ ,
__setattr__ ,
__str__
|
Inherited from Directive :
attach
|
|
ATTRIBUTE = ' function '
|
|
tagname = ' def '
|
|
args
|
|
defaults
|
|
name
|
Inherited from Directive :
expr
Inherited from object :
__class__
|
__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__
|