duck.template.templatetags

Module for creating user custom template tags and filters.

Module Contents

Classes

BlockTemplateTag

Template tag which acts as a block level template tag.

TemplateFilter

Base Class for template Filter.

TemplateTag

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.TemplateTag

Template 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

register_in_django(library)[source]

Register this tag in a Django template library.

Parameters:

library – The Django template library to register the tag with.

register_in_jinja2(environment)[source]

Register this tag as a block-level tag in a Jinja2 environment.

Parameters:

environment – The Jinja2 environment to register the tag with.

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.

register_in_django(library)[source]

Register this filter in a Django template library.

Parameters:

library – The Django template library to register the filter with.

register_in_jinja2(environment)[source]

Register this filter in a Jinja2 environment.

Parameters:

environment – The Jinja2 environment to register the filter with.

exception duck.template.templatetags.TemplateFilterError[source]

Bases: Exception

Exception 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.

register_in_django(library)[source]

Register this tag in a Django template library.

Parameters:

library – The Django template library to register the tag with.

register_in_jinja2(environment)[source]

Register this tag in a Jinja2 environment.

Parameters:

environment – The Jinja2 environment to register the tag with.

exception duck.template.templatetags.TemplateTagError[source]

Bases: Exception

Exception for errors related to the TemplateTag class.

Initialization

Initialize self. See help(type(self)) for accurate signature.