====================== Designer Documentation ====================== This part of the Jinja documentaton is meant for template designers. Basics ====== The Jinja template language is designed to strike a balance between content and application logic. Nevertheless you can use a python like statement language. You don't have to know how Python works to create Jinja templates, but if you know it you can use some additional statements you may know from Python. Here is a small example template: .. sourcecode:: html+jinja
{{ debug()|e }}A context isn't flat which means that each variable can has subvariables, as long as it is representable as python data structure. You can access attributes of a variable using the dot and bracket operators. The following examples show this: .. sourcecode:: jinja {{ user.username }} is the same as {{ user['username'] }} you can also use a variable to access an attribute: {{ users[current_user].username }} If you have numerical indices you have to use the [] syntax: {{ users[0].username }} Filters ======= In the examples above you might have noticed the pipe symbols. Pipe symbols tell the engine that it has to apply a filter on the variable. Here is a small example: .. sourcecode:: jinja {{ variable|replace('foo', 'bar')|escape }} If you want, you can also put whitespace between the filters. This will look for a variable `variable`, pass it to the filter `replace` with the arguments ``'foo'`` and ``'bar'``, and pass the result to the filter `escape` that automatically XML-escapes the value. The `e` filter is an alias for `escape`. Here is the complete list of supported filters: [[list_of_filters]] .. admonition:: note Filters have a pretty low priority. If you want to add fitered values you have to put them into parentheses. The same applies if you want to access attributes: .. sourcecode:: jinja correct: {{ (foo|filter) + (bar|filter) }} wrong: {{ foo|filter + bar|filter }} correct: {{ (foo|filter).attribute }} wrong: {{ foo|filter.attribute }} Tests ===== You can use the `is` operator to perform tests on a value: .. sourcecode:: jinja {{ 42 is numeric }} -> true {{ "foobar" is numeric }} -> false {{ 'FOO' is upper }} -> true These tests are especially useful when used in `if` conditions. [[list_of_tests]] Global Functions ================ Test functions and filter functions live in their own namespace. Global functions not. They behave like normal objects in the context. Beside the functions added by the application or framewhere there are two functions available per default: `range` Works like the python `range function`_ just that it doesn't support ranges greater than ``1000000``. `debug` Function that outputs the contents of the context. Loops ===== To iterate over a sequence, you can use the `for` loop. It basically looks like a normal Python `for` loop and works pretty much the same: .. sourcecode:: html+jinja
Welcome on my awsome homepage.
{% endblock %} The ``{% extends %}`` tag is the key here. It tells the template engine that this template "extends" another template. When the template system evaluates this template, first it locates the parent. The filename of the template depends on the template loader. For example the ``FileSystemLoader`` allows you to access other templates by giving the filename. You can access templates in subdirectories with an slash: .. sourcecode:: jinja {% extends "layout/default.html" %} But this behavior can depend on the application using Jinja. Note that since the child template didn't define the ``footer`` block, the value from the parent template is used instead. .. admonition:: Note You can't define multiple ``{% block %}`` tags with the same name in the same template. This limitation exists because a block tag works in "both" directions. That is, a block tag doesn't just provide a hole to fill - it also defines the content that fills the hole in the *parent*. If there were two similarly-named ``{% block %}`` tags in a template, that template's parent wouldn't know which one of the blocks' content to use. Template Inclusion ================== You can load another template at a given position using ``{% include %}``. Usually it's a better idea to use inheritance but if you for example want to load macros, `include` works better than `extends`: .. sourcecode:: jinja {% include "myhelpers.html" %} {{ my_helper("foo") }} If you define a macro called ``my_helper`` in ``myhelpers.html``, you can now use it from the template as shown above. Filtering Blocks ================ Sometimes it could be a good idea to filter a complete block of text. For example, if you want to escape some html code: .. sourcecode:: jinja {% filter escape %}goes here
{% endfilter %}
Of course you can chain filters too:
.. sourcecode:: jinja
{% filter lower|escape %}
SOME TEXT
{% endfilter %}
returns ``"<b>some text</b>"``.
Defining Variables
==================
You can also define variables in the namespace using the ``{% set %}`` tag:
.. sourcecode:: jinja
{% set foo = 'foobar' %}
{{ foo }}
This should ouput ``foobar``.
Scopes
======
Jinja has multiple scopes. A scope is something like a new transparent foil on
a stack of foils. You can only write to the outermost foil but read all of them
since you can look through them. If you remove the top foil all data on that
foil disappears. Some tags in Jinja add a new layer to the stack. Currently
these are `block`, `for`, `macro` and `filter`. This means that variables and
other elements defined inside a macro, loop or some of the other tags listed
above will be only available in that block. Here an example:
.. sourcecode:: jinja
{% macro angryhello name %}
{% set angryname = name|upper %}
Hello {{ name }}. Hello {{ name }}!
HELLO {{ angryname }}!!!!!!111
{% endmacro %}
The variable ``angryname`` just exists inside the macro, not outside it.
Defined macros appear on the context as variables. Because of this, they are
affected by the scoping too. A macro defined inside of a macro is just available
in those two macros (the macro itself and the macro it's defined in). For `set`
and `macro` two additional rules exist: If a macro is defined in an extended
template but outside of a visible block (thus outside of any block) will be
available in all blocks below. This allows you to use `include` statements to
load often used macros at once.
Undefined Variables
===================
If you have already worked with python you probably know about the fact that
undefined variables raise an exception. This is different in Jinja. There is a
special value called `undefined` that represents values that do not exist.
This special variable works complete different from any variables you maybe
know. If you print it using ``{{ variable }}`` it will not appear because it's
literally empty. If you try to iterate over it, it will work. But no items
are returned. Comparing this value to any other value results in `false`.
Even if you compare it to itself:
.. sourcecode:: jinja
{{ undefined == undefined }}
will return false. Not even undefined is undefined :)
Use `is defined` / `is not defined`:
{{ undefined is not defined }}
will return true.
There are also some additional rules regarding this special value. Any
mathematical operators (``+``, ``-``, ``*``, ``/``) return the operand
as result:
.. sourcecode:: jinja
{{ undefined + "foo" }}
returns "foo"
{{ undefined - 42 }}
returns 42. Note: not -42!
In any expression `undefined` evaluates to `false`. It has no length, all
attribute calls return undefined, calling too:
.. sourcecode:: jinja
{{ undefined.attribute().attribute_too[42] }}
still returns `undefined`.
Escaping
========
Sometimes you might want to add Jinja syntax elements into the template
without executing them. In that case you have quite a few possibilities.
For small parts this might be a good way:
.. sourcecode:: jinja
{{ "{{ foo }} is variable syntax and {% foo %} is block syntax" }}
When you have multiple elements you can use the ``raw`` block:
.. sourcecode:: jinja
{% raw %}
Filtering blocks works like this in Jinja:
{% filter escape %}
goes here
{% endfilter %}
{% endraw %}
Reserved Keywords
=================
Jinja has some keywords you cannot use a variable names. This limitation
exists to make look coherent. Syntax highlighters won't mess things up and
you will don't have unexpected output.
The following keywords exist and cannot be used as identifiers:
`and`, `block`, `cycle`, `elif`, `else`, `endblock`, `endfilter`,
`endfor`, `endif`, `endmacro`, `endraw`, `endtrans`, `extends`, `filter`,
`for`, `if`, `in`, `include`, `is`, `macro`, `not`, `or`, `pluralize`,
`raw`, `recursive`, `set`, `trans`
If you want to use such a name you have to prefix or suffix it or use
alternative names:
.. sourcecode:: jinja
{% for macro_ in macros %}
{{ macro_('foo') }}
{% endfor %}
If future Jinja releases add new keywords those will be "light" keywords which
means that they won't raise an error for several releases but yield warnings
on the application side. But it's very unlikely that new keywords will be
added.
Internationalization
====================
If the application is configured for i18n, you can define translatable blocks
for translators using the `trans` tag or the special underscore function:
.. sourcecode:: jinja
{% trans %}
this is a translatable block
{% endtrans %}
{% trans "This is a translatable string" %}
{{ _("This is a translatable string") }}
The latter one is useful if you want translatable arguments for filters etc.
If you want to have plural forms too, use the `pluralize` block:
.. sourcecode:: jinja
{% trans users=users %}
One user found.
{% pluralize %}
{{ users }} users found.
{% endtrans %}
{% trans first=(users|first).username|escape, user=users|length %}
one user {{ first }} found.
{% pluralize users %}
{{ users }} users found, the first one is called {{ first }}.
{% endtrans %}
If you have multiple arguments, the first one is assumed to be the indicator (the
number that is used to determine the correct singular or plural form. If you
don't have the indicator variable on position 1 you have to tell the `pluralize`
tag the correct variable name.
Inside translatable blocks you cannot use blocks or expressions (however you can
still use the ``raw`` block which will work as expected). The variable
print syntax (``{{ variablename }}``) is the only way to insert the variables
defined in the ``trans`` header. Filters must be applied in the header.
.. admonition:: note
Please make sure that you always use pluralize blocks where required.
Many languages have more complex plural forms than the English language.
Never try to workaround that issue by using something like this:
.. sourcecode:: jinja
{% if count != 1 %}
{{ count }} users found.
{% else %}
one user found.
{% endif %}
.. _slicing chapter: http://diveintopython.org/native_data_types/lists.html#odbchelper.list.slice
.. _range function: http://docs.python.org/tut/node6.html#SECTION006300000000000000000