The simplest form of expressions are literals. Literals are representations
for Python objects such as strings and numbers. The following literals exist:
-``"Hello World"``
+"Hello World":
Everything between two double or single quotes is a string. They are
useful whenever you need a string in the template (for example as
arguments to function calls, filters or just to extend or include a
template).
-``42`` / ``42.23``
+42 / 42.23:
Integers and floating point numbers are created by just writing the
number down. If a dot is present the number is a float, otherwise an
integer. Keep in mind that for Python ``42`` and ``42.0`` is something
different.
-``['list', 'of', 'objects']``
+['list', 'of', 'objects']:
Everything between two brackets is a list. Lists are useful to store
sequential data in or to iterate over them. For example you can easily
create a list of links using lists and tuples with a for loop.
{% endfor %}
</ul>
-``('tuple', 'of', 'values')``
+('tuple', 'of', 'values'):
Tuples are like lists, just that you can't modify them. If the tuple
only has one item you have to end it with a comma. Tuples are usually
used to represent items of two or more elements. See the example above
for more details.
-``{'dict': 'of', 'keys': 'and', 'value': 'pairs'}``
+{'dict': 'of', 'keys': 'and', 'value': 'pairs'}:
A dict in Python is a structure that combines keys and values. Keys must
be unique and always have exactly one value. Dicts are rarely used in
templates, they are useful in some rare cases such as the :func:`xmlattr`
filter.
-``true`` and ``false``
+true / false:
true is always true and false is always false. Keep in mind that those
literals are lowercase!
Jinja allows you to calculate with values. This is rarely useful in templates
but exists for completeness sake. The following operators are supported:
-``+``
+\+
Adds two objects with each other. Usually numbers but if both objects are
strings or lists you can concatenate them this way. This however is not
the preferred way to concatenate strings! For string concatenation have
a look at the ``~`` operator. ``{{ 1 + 1 }}`` is ``2``.
-``-``
+\-
Substract two numbers from each other. ``{{ 3 - 2 }}`` is ``1``.
-``/``
+/
Divide two numbers. The return value will be a floating point number.
``{{ 1 / 2 }}`` is ``{{ 0.5 }}``.
-``//``
+//
Divide two numbers and return the truncated integer result.
``{{ 20 / 7 }}`` is ``2``.
-``%``
+%
Calculate the remainder of an integer division between the left and right
operand. ``{{ 11 % 7 }}`` is ``4``.
-``*``
+\*
Multiply the left operand with the right one. ``{{ 2 * 2 }}`` would
return ``4``. This can also be used to repeat string multiple times.
``{{ '=' * 80 }}`` would print a bar of 80 equal signs.
-``**``
+\**
Raise the left operand to the power of the right operand. ``{{ 2**3 }}``
would return ``8``.
For `if` statements / `for` filtering or `if` expressions it can be useful to
combine group multiple expressions:
-``and``
+and
Return true if the left and the right operand is true.
-``or``
+or
Return true if the left or the right operand is true.
-``not``
+not
negate a statement (see below).
-``()`` (group)
+(expr)
group an expression.
Note that there is no support for any bit operations or something similar.
The following operators are very useful but don't fit into any of the other
two categories:
-``in``
+in
Perform sequence / mapping containment test. Returns true if the left
operand is contained in the right. ``{{ 1 in [1, 2, 3] }}`` would for
example return true.
-``is``
- Performs a :ref:`tests <test>`.
+is
+ Performs a :ref:`test <tests>`.
-``|``
- Applies a :ref:`filters <filter>`.
+\|
+ Applies a :ref:`filter <filters>`.
-``~``
+~
Converts all operands into strings and concatenates them.
``{{ "Hello " ~ name ~ "!" }}`` would return (assuming `name` is
``'John'``) ``Hello John!``.
-``()`` (call)
+()
Call a callable: ``{{ post.render() }}``. Inside of the parentheses you
can use arguments and keyword arguments like in python:
``{{ post.render(user, full=true) }}``.
-``.`` / ``[]``
+. / []
Get an attribute of an object. (See :ref:`variables`)
for method in '__getitem__', '__getslice__', 'capitalize', \
'title', 'lower', 'upper', 'replace', 'ljust', \
'rjust', 'lstrip', 'rstrip', 'center', 'strip', \
- 'translate', 'expandtabs', 'rpartition', 'swapcase', \
- 'zfill':
+ 'translate', 'expandtabs', 'swapcase', 'zfill':
locals()[method] = make_wrapper(method)
# new in python 2.5
if hasattr(unicode, 'partition'):
- locals().update(partition=make_wrapper('partition'),
- rpartition=make_wrapper('rpartition'))
+ locals().update(
+ partition=make_wrapper('partition'),
+ rpartition=make_wrapper('rpartition')
+ )
del method, make_wrapper