defernia.web.helpers — Template helpers

This module contains several useful helpers for templating. To install it into the Jinja environment, use install() function.

class defernia.web.helpers.RegisterDecorator(dictionary)

Decorator function template. It is for making filter(), test() and func() decorator functions.

defernia.web.helpers.filter = <defernia.web.helpers.RegisterDecorator object at 0x54877d0>

The decorator function that registers a function as a template filter:

@filter
def greet(value):
    return u'Hello, ' + unicode(value)

You can specify the custom name for it:

@filter('hello')
def greet(value):
    return u'Hello, ' + unicode(value)
defernia.web.helpers.test = <defernia.web.helpers.RegisterDecorator object at 0x5487810>

The decorator function that registers a function as a template test (predicate). For example:

@test
def digit(value):
    return value.isdigit()

You can specify the custom name for it:

@test('digit')
def is_digit(value):
    return value.isdigit()
defernia.web.helpers.func = <defernia.web.helpers.RegisterDecorator object at 0x5487850>

The decorator function that registers a function as a template global function:

@func
def code(value):
    return u'<code>{0}</code>'.format(value)

You can specify the custom name for it as well as filter() or test() decorators.

defernia.web.helpers.install(environment)

Install the helpers defined above into the environment.

Parameters:environment (jinja2.Environment) – the Jinja environment
defernia.web.helpers.require(import_path)

Imports a module or an object inside it by its import path.

{% set datetime = require('datetime') %}
{% set user = require('defernia.user') %}

You can specify an object inside a module as well:

{% set date = require('datetime:date') %}
{% set User = require('defernia.user:User') %}
Parameters:import_path (basestring) – a dot-separated standard Python import path
Returns:an imported module or object

See also

Function werkzeug.utils.import_string()
Imports an object based on a string.

Project Versions

Previous topic

defernia.web.user — User web pages

Next topic

defernia.web.serializers — Serializers for various content types

This Page