Template Tags & Filters

Custom Django Template Tags and Filters.

awl.templatetags.awltags.accessor(parser, token)

This template tag is used to do complex nested attribute accessing of an object. The first parameter is the object being accessed, subsequent paramters are one of:

  • a variable in the template context

  • a literal in the template context

  • either of the above surrounded in square brackets

For each variable or literal parameter given a getattr is called on the object, chaining to the next parameter. For any sqaure bracket enclosed items the access is done through a dictionary lookup.

Example:

{% accessor car where 'front_seat' [position] ['fabric'] %}

The above would result in the following chain of commands:

ref = getattr(car, where)
ref = getattr(ref, 'front_seat')
ref = ref[position]
return ref['fabric']

This tag also supports “as” syntax, putting the results into a template variable:

{% accessor car 'interior' as foo %}
awl.templatetags.awltags.getitem(dictionary, keyvar)

Custom django template filter that allows access to an item of a dictionary through the key contained in a template variable. Example:

context_data = {
    'data':{
        'foo':'bar',
    },
    'key':'foo',
}

template = Template('{% load awltags %}{{data|getitem:key}}')
context = Context(context_data)
result = template.render(context)

>>> result
'bar'

Note

Any KeyErrors are ignored and return an empty string

awl.templatetags.awltags.jsonify(value)

This tag takes an object in the context and converts it to JSON, inlining the resulting code.

Example:

<script>
    let actors = {% jsonify hollywood.actors %};
</script>

Note that this does not return an enclosed string but the actual JSON, its primary use is to turn a dictionary in the context into a usable Javascript object. If you want it in string form, you need to enclose the tag in quotes in your Javascript code.

awl.templatetags.awltags.nop(*args)

This tag does nothing. Useful for a comment without having to build a full comment block. All parameters are ignored.

Example:

{% nop 'this is a string' %}

The Django template engine now supports single line comments using the {# and #} braces. You should use those instead of this tag.

awl.templatetags.awltags.qif(parser, token)

A simplified single tag that does if matching. Takes a condition and a result if the condition is true. Uses Django’s parser for the regular {% if %} tag so any conditions supported by it should work here.

{% qif value >= 2 'plural' %}

In the above example, if value is greater than or equal to 2, then the tag renders as “plural”, otherwise returns empty.

awl.templatetags.awltags.qifelse(parser, token)

A simplified single tag that does if-else matching. Takes a condition, a result if the condition is true, and a result if the condition is false. Uses Django’s parser for the regular {% if %} tag so any conditions supported by it should work here.

{% qifelse value >= 2 'plural' 'singular' %}

In the above example, if value is greater than or equal to 2, then the tag renders as “plural”, otherwise returns “singular”.