genshi.template.directives.DefDirective

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) x.__init__(...) initializes x; see x.__class__.__doc__ for signature |
__call__(self, stream, directives, ctxt, **vars) Apply the directive to the given stream. |
__repr__(self) repr(x) |
Inherited from `object`: `__delattr__`, `__getattribute__`, `__hash__`, `__new__`, `__reduce__`, `__reduce_ex__`, `__setattr__`, `__str__`

Class Methods
attach(cls, template, stream, value, namespaces, pos) Called after the template stream has been completely parsed. |

Class Variables
| tagname = ``'``def``'``

Properties
| args
| defaults
| dstar_args
| name
| star_args
Inherited from `Directive`: `expr` Inherited from `object`: `__class__`

Method Details

### __init__(self, args, template, namespaces=None, lineno=-1, offset=-1) (Constructor) |
x.__init__(...) initializes x; see x.__class__.__doc__ for signature Overrides: object.__init__ (inherited documentation)

### 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)

### __repr__(self) (Representation operator) |
repr(x) Overrides: object.__repr__ (inherited documentation)