Jinja2 uses a central object called the template :class:`Environment`.
Instances of this class are used to store the configuration, global objects
and are used to load templates from the file system or other locations.
-Even if you are creating templates from string by using the constructor of
+Even if you are creating templates from strings by using the constructor of
:class:`Template` class, an environment is created automatically for you,
albeit a shared one.
`str` type and the other is the `unicode` type, both of which extend a type
called `basestring`. Unfortunately the default is `str` which should not
be used to store text based information unless only ASCII characters are
-used. With Python 2.6 it is possible to my `unicode` the default on a per
+used. With Python 2.6 it is possible to make `unicode` the default on a per
module level and with Python 3 it will be the default.
To explicitly use a unicode string you have to prefix the string literal
some operations. All parameters except of `hint` should be provided
as keyword parameters for better readability. The `hint` is used as
error message for the exception if provided, otherwise the error
- message generated from `obj` and `name` automatically. The exception
+ message will be generated from `obj` and `name` automatically. The exception
provided as `exc` is raised if something with the generated undefined
object is done that the undefined object does not allow. The default
exception is :exc:`UndefinedError`. If a `hint` is provided the
publication date: {{ article.pub_date|datetimeformat('%d-%m-%Y') }}
Filters can also be passed the current template context or environment. This
-is useful if a filters wants to return an undefined value or check the current
+is useful if a filter wants to return an undefined value or check the current
:attr:`~Environment.autoescape` setting. For this purpose two decorators
exist: :func:`environmentfilter` and :func:`contextfilter`.
sudo pip install Jinja2==dev
-.. _download page: http://jinja.pocoo.org/2/download
+.. _download page: http://pypi.python.org/pypi/Jinja2
.. _setuptools: http://peak.telecommunity.com/DevCenter/setuptools
.. _easy_install: http://peak.telecommunity.com/DevCenter/EasyInstall
.. _pip: http://pypi.python.org/pypi/pip
``{{ name|striptags|title }}`` for example will remove all HTML Tags from the
`name` and title-cases it. Filters that accept arguments have parentheses
around the arguments, like a function call. This example will join a list
-by spaces: ``{{ list|join(', ') }}``.
+by commas: ``{{ list|join(', ') }}``.
The :ref:`builtin-filters` below describes all the builtin filters.
.. admonition:: Note
In Jinja 2.0 the context that was passed to the included template
- did not include variables define in the template. As a matter of
+ did not include variables defined in the template. As a matter of
fact this did not work::
{% for box in boxes %}
~~~~
Jinja allows you to calculate with values. This is rarely useful in templates
-but exists for completeness sake. The following operators are supported:
+but exists for completeness' sake. The following operators are supported:
\+
- Adds two objects with each other. Usually numbers but if both objects are
+ Adds two objects together. Usually the objects are numbers but if both 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``.
+ Substract the second number from the first one. ``{{ 3 - 2 }}`` is ``1``.
/
Divide two numbers. The return value will be a floating point number.
``{{ 20 / 7 }}`` is ``2``.
%
- Calculate the remainder of an integer division between the left and right
- operand. ``{{ 11 % 7 }}`` is ``4``.
+ Calculate the remainder of an integer division. ``{{ 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.
+ return ``4``. This can also be used to repeat a string multiple times.
``{{ '=' * 80 }}`` would print a bar of 80 equal signs.
\**
Logic
~~~~~
-For `if` statements / `for` filtering or `if` expressions it can be useful to
-combine group multiple expressions:
+For `if` statements, `for` filtering or `if` expressions it can be useful to
+combine multiple expressions:
and
Return true if the left and the right operand is true.
()
Call a callable: ``{{ post.render() }}``. Inside of the parentheses you
- can use arguments and keyword arguments like in python:
+ can use positional arguments and keyword arguments like in python:
``{{ post.render(user, full=true) }}``.
. / []
loops or over multiple loops.
This is for example very useful if you want to show a list of folders and
- files, with the folders on top, but both in the same list with alteranting
+ files, with the folders on top, but both in the same list with alternating
row colors.
The following example shows how `cycler` can be used::
def make_module(self, vars=None, shared=False, locals=None):
"""This method works like the :attr:`module` attribute when called
- without arguments but it will evaluate the template every call
- rather then caching the template. It's also possible to provide
+ without arguments but it will evaluate the template on every call
+ rather than caching it. It's also possible to provide
a dict which is then used as context. The arguments are the same
as for the :meth:`new_context` method.
"""
def do_filesizeformat(value, binary=False):
"""Format the value like a 'human-readable' file size (i.e. 13 KB,
4.1 MB, 102 bytes, etc). Per default decimal prefixes are used (mega,
- giga etc.), if the second parameter is set to `True` the binary
- prefixes are (mebi, gibi).
+ giga, etc.), if the second parameter is set to `True` the binary
+ prefixes are used (mebi, gibi).
"""
bytes = float(value)
base = binary and 1024 or 1000
def do_slice(value, slices, fill_with=None):
"""Slice an iterator and return a list of lists containing
those items. Useful if you want to create a div containing
- three div tags that represent columns:
+ three ul tags that represent columns:
.. sourcecode:: html+jinja
{%- for row in items|batch(3, ' ') %}
<tr>
{%- for column in row %}
- <tr>{{ column }}</td>
+ <td>{{ column }}</td>
{%- endfor %}
</tr>
{%- endfor %}