Filters
A filter changes a value as it is written into a document. Put it after the value with |, and
give it any values it needs in brackets:
{{ form.DraftFormName|upper }}
{{ form.als_fields_set.count() }} field{{ form.als_fields_set.count()|pluralize }}
Filters can follow one another, each working on what the one before it gave. To apply a filter to a
whole block of a template rather than one value, use {% filter %}:
{% filter upper %}
This text is written in capitals.
{% endfilter %}
This page lists every filter a Document Template can use. Any other filter name stops the template with an error.
The examples use three values a real template would get from its data: when, a date and time of
5 March 2026 14:30 UTC; forms, three Forms with OIDs DM, AE and VS, of which AE is inactive; and
form, the first of them. The Gives column is exactly what each example writes into the
document.
Values are escaped
Text written into a document has &, <, > and quotes replaced by their escaped forms,
so a value cannot break the document's markup. This is why Fish & Chips is written as
Fish & Chips. Filters that produce markup on purpose, such as markdown and
clean_html, mark their result as safe so it is not escaped a second time.
This happens in every type of Template, Text included. A Text Template that writes plain text,
Markdown or CSV rather than XML should write its values with safe, as in
{{ form.DraftFormName|safe }}, so that they appear exactly as they are stored.
Two things are not escaped. The text the {% ai %} tag writes is used exactly as it comes back,
and The AI tag lists the filters to use on it. The result of a
{% filter %} block is not escaped again either, but the values inside it already were, so a
filter that changes letters can break them: {% filter upper %}{{ "Fish & Chips" }}{% endfilter %}
writes FISH & CHIPS. Apply such a filter to the value instead: {{ "Fish & Chips"|upper }}.
Filters added by TrialGrid
Filter |
What it does |
Example |
Gives |
|---|---|---|---|
|
Splits a list into lists of the length you give. The last one holds what is left. |
|
|
|
Keeps only the HTML tags you list and removes the others, keeping their text. With no list, keeps a standard set of text formatting tags. On a Word, Excel or PDF Template, follow it with |
|
|
|
Turns HTML into Word or Excel formatting, so bold, lists and paragraphs appear as they would on a web page. Only in Word and Excel Templates. |
||
|
Writes a date and time as year, month and day. Gives nothing for an empty value. |
|
|
|
Writes a date and time in the format you give, using the codes in Python Date Formats. Gives nothing for an empty value. |
|
|
|
Removes HTML tags and attributes that are not on a fixed list of safe formatting, keeping their text. |
|
|
|
Writes a date and time in the ISO 8601 format. Gives nothing for an empty value. |
|
|
|
Escapes the text, then turns each line break into |
|
|
|
Turns Markdown into HTML. Any HTML already in the text is removed first. On a Word or Excel Template, follow it with |
|
|
|
Writes a decimal number without trailing zeros or scientific notation. Any other value is written as it is. |
|
|
|
Applied to a number, gives |
|
|
|
Joins the lines of a text into one line, with a space between each. HTML in the text is escaped, so it is shown rather than used. |
|
|
|
Turns HTML into the markup a PDF Template's paragraphs accept. Links are removed and their text kept, unless you pass |
|
|
|
Marks JSON text as safe to place inside a |
|
|
|
Turns text into lowercase letters, numbers and hyphens, suitable for a file name or an anchor. |
|
|
|
Removes HTML tags, leaving their text. |
|
|
|
Converts a date and time to the time zone you name. See List of Time Zones. |
|
|
|
Writes a date and time with the name of its time zone. It does not convert the time: use |
|
|
|
Escapes |
|
|
convert_html has no example because what it gives is Word or Excel markup. See
Generating Word Documents and
Generating Excel Documents.
Filters from Jinja
These come with Jinja, the template language Document Templates are written in. The Jinja documentation describes each one in more depth.
Filter |
What it does |
Example |
Gives |
|---|---|---|---|
|
The number without its sign. |
|
|
|
An attribute of an object, by name. |
|
|
|
Splits a list into lists of the length you give. A second value fills out the last one. |
|
|
|
The first letter upper case and the rest lower case. |
|
|
|
The text centred in the width you give. |
|
|
|
Another name for |
|
|
|
Another name for |
|
|
|
The value you give if the value is undefined. Pass |
|
|
|
The items of a dictionary sorted by key, or by value with |
|
|
|
Another name for |
|
|
|
Escapes |
|
|
|
A number of bytes as a readable size. |
|
|
|
The first item. |
|
|
|
The value as a decimal number. |
|
|
|
Escapes for HTML even a value already marked safe. |
|
|
|
Fills a printf-style pattern with the values you give. |
|
|
|
Groups a list of objects by an attribute. Each group has a |
|
|
|
Indents every line but the first by the number of spaces you give. The example shows its line break as |
|
|
|
The value as a whole number. |
|
|
|
The key and value pairs of a dictionary, for a loop. |
|
|
|
The items of a list joined with the text you give. |
|
|
|
The last item. |
|
|
|
How many items, or how many characters in a text. On a set of objects, use |
|
|
|
The value as a list. |
|
|
|
The text in lower case. |
|
|
|
One attribute of each object ( |
|
|
|
The largest item. |
|
|
|
The smallest item. |
|
|
|
The value laid out for reading, for checking what a value holds. |
|
|
|
An item chosen at random. |
||
|
The items that fail a test. |
|
|
|
The objects whose attribute fails a test, or is false if you name no test. |
|
|
|
The text with every occurrence of one piece replaced by another. |
|
|
|
The items, or the characters of a text, in the opposite order. |
|
|
|
The number rounded to the places you give. |
|
|
|
Marks a value as safe, so it is not escaped. Use it on a value you know cannot break the document's markup, or in a Text Template whose output is not markup. |
|
|
|
The items that pass a test. |
|
|
|
The objects whose attribute passes a test, or is true if you name no test. |
|
|
|
Splits a list into the number of columns you give. |
|
|
|
The items in order. |
|
|
|
The value as text. |
|
|
|
Removes HTML tags and runs of spaces. |
|
|
|
The items added up. |
|
|
|
Each word starting with a capital letter. |
|
|
|
The value as JSON, safe to place inside a |
|
|
|
The text without spaces at the start and end. |
|
|
|
The text cut to the length you give, ending in |
|
|
|
The items without duplicates. |
|
|
|
The text in upper case. |
|
|
|
The text encoded for use in a web address. |
|
|
|
Turns web addresses in a text into HTML links. On a Word or Excel Template, follow it with |
|
|
|
How many words in a text. |
|
|
|
Wraps a text at the width you give, with a line break or the |
|
|
|
Turns a dictionary into HTML or XML attributes. |
|
|
random has no example because what it gives changes every time.