duck.template.templatetags¶
Module for creating user custom template tags and filters.
Module Contents¶
Classes¶
Template tag which acts as a block level template tag. |
|
Base Class for template Filter. |
|
Base class for defining and registering template tags. |
API¶
- class duck.template.templatetags.BlockTemplateTag(tagname: str, tagcallable: Callable, takes_context: bool = False)[source]¶
Bases:
duck.template.templatetags.TemplateTagTemplate tag which acts as a block level template tag.
To use this, name and tagcallable arguments should be provided.
Notes:
The tagcallable should be a callable object accepting context as first argument if takes_context=True, and the second argument as the content wrapped between the tags.
The tagcallable should always return the new modified content after processing.
Example:
def do_something(content): # process and modify content logic return content tag = BlockTemplateTag("some_name", tagcallable=do_something)Initialization
- class duck.template.templatetags.TemplateFilter(filtername: str, filtercallable: Callable)[source]¶
Base Class for template Filter.
This will be used to register this filter to a template.
Notes: - TemplateFilter does not support takes_context. You need to parse the context directly if you need it.
Example: ```django {{ variable | myfilter:context }} ```Form:
{{ variable | myfilter }}Initialization
- __all_filters¶
‘defaultdict(…)’
- __repr__()[source]¶
Returns a string representation of the TemplateFilter.
- Returns:
String representation of the TemplateFilter.
- Return type:
str
- classmethod get_filter(filtername: str)[source]¶
Retreive a created Template Filter with provided name.
- Raises:
TemplateFilterError – If filter with provided name does’nt exist.
- exception duck.template.templatetags.TemplateFilterError[source]¶
Bases:
ExceptionException for errors related to the TemplateFilter class.
Initialization
Initialize self. See help(type(self)) for accurate signature.
- class duck.template.templatetags.TemplateTag(tagname: str, tagcallable: Callable, takes_context: bool = False)[source]¶
Base class for defining and registering template tags.
This class provides a unified way to register template tags in templating engines like Django or Jinja2. It supports callable tags, allowing flexible logic to be associated with the tag.
Usage: 1. Define a callable function to handle the tag logic:
```py def do_something(arg): # Perform some operations with the argument return f"Processed: {arg}" ``` 2. Register the callable as a template tag: ```py tag = TemplateTag("my_tag", tagcallable=do_something) ```Supported Syntax:
Django:
{% my_tag arg1 arg2 %}Jinja2:
{{ my_tag(arg1, arg2) }}
Initialization
- __all_tags¶
‘defaultdict(…)’
- __repr__()[source]¶
Returns a string representation of the TemplateTag.
- Returns:
String representation of the TemplateTag.
- Return type:
str
- classmethod get_tag(tagname: str)[source]¶
Retreive a created Template Tag with provided name.
- Raises:
TemplateTagError – If tag with provided name does’nt exist.