Step: jinja#
Renders requested Jinja2 template with document context and optionally other data.
Input#
If not used as a first step, then the previous document is available from document variable.
Output#
Results to a file of specified type (via content-type option) and file extension (via extension option).
Options#
template= path to template file to be renderedcontent-type= MIME type of resulting fileextension= file extension for the produced file (without leading dot)
Optional:
jinja-ext= comma-separated list of Jinja2 extensions to be enabled (supported values:debug)i18n-dir= location (relative to template root) of translationsi18n-domain= domain string of translationsi18n-lang= language code used in the templateextras= comma-separated list of related entities to query in addition to Document Context (possible values:submissions,questionnaire); values will be added toextrasattribute of the document context
Template (Jinja2)#
Variables#
The following variables are set:
ctx= contains JSON-like plain Document Context (possibly withextrasattribute, if configured)secrets= dictionary of secret values, only if enabled by configuration filerequests= wrapper of requests module only if enabled by configuration file
Filters#
Within Jinja templates, you can use so-called filters.Basically, those are functions applied to a first argument using pipe | symbol.
Builtin Filters#
There are several widely used builtin filters directly in Jinja.
Value Conversion#
We provide several filters that can be used for conversion of values:
datetime_format= Formats timestampExample:
x.created_at|datetime_format("%d/%m/%y")Arguments:
iso_timestamp-datetimeor ISO 8601strfmt- datetime format passed to strftime
of_alphabet= Converts integer to charactersExample:
x|of_alphabetIt prints
a(for 0) tozand then continues withaa,ab, etc.Arguments:
n- integer >= 0, usually some index
roman= Converts integer to Roman numeralExample:
x|romanArguments:
n- integer >= 0, usually some index
markdown= Converts markdown to HTMLExample:
x|romanArguments:
md_text- string containing Markdown syntax
dot= Ends sentence if not already endedExample:
"This sentence has no end"|dotArguments:
text
extract= Extracts values from object by having keysExample:
entities.questions|extract([uuid1, uuid2, uuid3])Arguments:
obj- object for getting values (typicallydict)keys- list of keys to retrieve
Special#
These filters are more complex and add various support to template development.
to_context_obj= Converts plain context to well-defined objectsThis filter is used for easier transition and might be removed in the future.
Arguments:
ctx- plain JSON-like document context
Tests#
Within Jinja templates, you can use so-called tests. Basically, those are helpers usable in conditions after is keyword:
{% if loop.index is divisibleby 3 %}
{# ... #}
{% endif %}
Builtin Tests#
There are several widely used builtin tests directly in Jinja.
Custom Tests#
not_empty= Checks if size of a collection is higher than 0Example:
items is not_empty
of_type= Checks if an object is instance of a certain type / classThe name must be a string; however, it is case-insensitive. It also checks all superclasses.
Example:
parent is of_type "ListQuestion"
Notes#
All paths (e.g. for
importorextendsin Jinja2 templates are relative from the template root, i.e. directory withtemplate.json).The
doJinja2 extension is enabled.Using file extension
.j2or.jinja2for templates is just a convention.The document context is provided in
ctxvariable, other variables, filters, and tests are documented in other documents.
Example#
{
"name" : "jinja",
"options" : {
"template" : "src/default.html.j2",
"content-type" : "text/html",
"extension" : "html"
}
}