Library¶
Provides a class for defining custom template utility libraries.
-
class
knights.library.
Library
¶ -
tags = {}
A dict of template tag handler functions
-
helpers = {}
A dict of helpers to be added to the template modules global scope.
-
Custom tags¶
To define a custom tag or helper, you first need a library.
from knights.library import Library
register = Library()
You must call the instance regsiser
as the Parser will only look for that
name, currently.
Next, you can register helpers as follows:
@register.helper
def addone(value):
return value + 1
If the name you want to use is reserved or a builtin, you can pass it to the decorator:
@register.helper(name='sun')
def addone(value):
return value + 1
Custom tag handlers are more complex, as they require you to construct AST. Howerver, they are just as simple to register.
@register.tag
def mytag(parser, token):
...
Tags are parsed the parser, and the rest of the token text after their name was split from the front.