Skip to content
This repository has been archived by the owner on Jun 4, 2022. It is now read-only.

Templating Reference

Adrian Cochrane edited this page May 26, 2018 · 22 revisions

For syntax, refer to Django's documentation.

{% autoescape mode %}

Sets the autoescaping mode. mode may be one of 'off', 'html', 'js-string', 'csv', or to restore the previous mode 'end'.

{% debug %}

Outputs all variables accessible at this point in the template as a <dl> list. Intended for debugging purposes, not production.

{% fetch %}

Evaluates the contained template until {% each %} to obtain a whitespace seperated list of HTTP or HTTPS URLs to download concurrently. Template rendering resumes after {% endfetch %} once all URLs have been downloaded.

{% each as name %}

For each HTTP response obtained from fetch, each renders it's contained template until {% endfetch %} with the parsed response stored in name.

The response is parsed as TSV or JSON (XML support is planned) according to the MIMEtype given by the server. If that MIMEtype is absent, it will be inferred.

{% filter filters %}

Applies filters to the contained template until {% endfilter %}. filters uses templating syntax, without the variable path or the preceding |.

{% firstof variables... %}

Finds the first value of variables... that is truthy.

{% for [key] value in variable %}

Evaluates the contained template until {% endfor %} (or {% empty %}) for each item contained in variable. Each time it assigns the item to value (which must be a global name), and the corrosponding key to key.

{% empty %}

Evaluates the contained template until {% endfor %} if there are no items in variable.

{% if condition... %}

Evaluates the condition, and if truthy evaluates the contained template until {% endif %} (or {% else %} or {% elif %}). The condition is a variable, or variables with infix operators (documented next) between them. Spaces are required between variables and operators.

Operators

In order of precedance (lowest binding to highest binding):

  • or
  • and
  • not (prefix)
  • <, >, <=, >=, ==, !=, in

Any operator may be prefixed with not to negate it (only really useful for in).

{% else %}

If the expression evaluated to something falsy, evaluates the contained template until {% endif %}.

{% elif alt-condition... %}

If the expression evaluated to something falsy and the contained template until the next {% elif %} or {% endif %}.

{% ifchanged variables... %}

Checks if any of the variables have changed since the last time this code was run. If so it runs the embedded template until {% endif %} (or {% else %}).

{% else %}

Evaluates if there are no changes.

{% now "format" %}

Outputs the time, formatted as per format. See Vala's documentation for how to write format strings.

{% templatetag type %}

As per Django.

{% trans [[name=]variable]... %}

Translates the contained template until {% endtrans %} (or {% plural %}), possibly using the additional variables specified in the tag. It is courtious to avoid markup and templating where possible within the the body of a trans tag. A preceding comment within this template will be split out specially for translators.

{% plural %}

Evaluates (in English) if the count variable indicates the plural form should be used.

{% verbatim %}

Outputs the contained text until {% endverbatim %} without evaluating any templating.

{% with %}

Evaluates the contained template using the given additional variables.

Filters

Where not documented, these should be self-describing. If they're not, please document them here.

|add

|capfirst

|cut

|date

|default

|escape

|filesize

|first

|force-escape

|join

|last

|length

|lengthis

|lower

|safe

|title