Skip to content
Myr

Applied documentation patches by Clemens Hermann.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

--HG-- branch : trunk

authored by Armin Ronacher · committed by Armin Ronacher · parent 02b42a8 · Browse files · 5 files+22 added−23 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+22 added−23 removed

  • other:+22 added−23 removed

5 files+22 added−23 removed

modified:docs/api.rst+4 added-4 removed
12 unmodified lines
1314Jinja2 uses a central object called the template :class:`Environment`.15Instances of this class are used to store the configuration, global objects16and are used to load templates from the file system or other locations.17Even if you are creating templates from string by using the constructor of17Even if you are creating templates from strings by using the constructor of18:class:`Template` class, an environment is created automatically for you,19albeit a shared one.2021Most applications will create one :class:`Environment` object on application
38 unmodified lines
60Python 2.x supports two ways of representing string objects. One is the61`str` type and the other is the `unicode` type, both of which extend a type62called `basestring`. Unfortunately the default is `str` which should not63be used to store text based information unless only ASCII characters are64used. With Python 2.6 it is possible to my `unicode` the default on a per64used. With Python 2.6 it is possible to make `unicode` the default on a per65module level and with Python 3 it will be the default.6667To explicitly use a unicode string you have to prefix the string literal68with a `u`: ``u'Hänsel und Gretel sagen Hallo'``. That way Python will
91 unmodified lines
160 for filters or functions that may return undefined objects for161 some operations. All parameters except of `hint` should be provided162 as keyword parameters for better readability. The `hint` is used as163 error message for the exception if provided, otherwise the error164 message generated from `obj` and `name` automatically. The exception164 message will be generated from `obj` and `name` automatically. The exception165 provided as `exc` is raised if something with the generated undefined166 object is done that the undefined object does not allow. The default167 exception is :exc:`UndefinedError`. If a `hint` is provided the168 `name` may be ommited.
370 unmodified lines
539 written on: {{ article.pub_date|datetimeformat }}540 publication date: {{ article.pub_date|datetimeformat('%d-%m-%Y') }}541542Filters can also be passed the current template context or environment. This543is useful if a filters wants to return an undefined value or check the current543is useful if a filter wants to return an undefined value or check the current544:attr:`~Environment.autoescape` setting. For this purpose two decorators545exist: :func:`environmentfilter` and :func:`contextfilter`.546547Here a small example filter that breaks a text into HTML line breaks and
120 unmodified lines
modified:docs/intro.rst+1 added-1 removed
99 unmodified lines
100Or the new `pip`_ command::101102 sudo pip install Jinja2==dev103104.. _download page: http://jinja.pocoo.org/2/download104.. _download page: http://pypi.python.org/pypi/Jinja2105.. _setuptools: http://peak.telecommunity.com/DevCenter/setuptools106.. _easy_install: http://peak.telecommunity.com/DevCenter/EasyInstall107.. _pip: http://pypi.python.org/pypi/pip108.. _mercurial: http://www.selenic.com/mercurial/
43 unmodified lines
modified:docs/templates.rst+11 added-12 removed
107 unmodified lines
108109``{{ name|striptags|title }}`` for example will remove all HTML Tags from the110`name` and title-cases it. Filters that accept arguments have parentheses111around the arguments, like a function call. This example will join a list112by spaces: ``{{ list|join(', ') }}``.112by commas: ``{{ list|join(', ') }}``.113114The :ref:`builtin-filters` below describes all the builtin filters.115116.. _tests:
718 unmodified lines
835836.. admonition:: Note837838 In Jinja 2.0 the context that was passed to the included template839 did not include variables define in the template. As a matter of839 did not include variables defined in the template. As a matter of840 fact this did not work::841842 {% for box in boxes %}843 {% include "render_box.html" %}
69 unmodified lines
913Math914~~~~915916Jinja allows you to calculate with values. This is rarely useful in templates917but exists for completeness sake. The following operators are supported:917but exists for completeness' sake. The following operators are supported:918919\+920 Adds two objects with each other. Usually numbers but if both objects are920 Adds two objects together. Usually the objects are numbers but if both are921 strings or lists you can concatenate them this way. This however is not922 the preferred way to concatenate strings! For string concatenation have923 a look at the ``~`` operator. ``{{ 1 + 1 }}`` is ``2``.924925\-926 Substract two numbers from each other. ``{{ 3 - 2 }}`` is ``1``.926 Substract the second number from the first one. ``{{ 3 - 2 }}`` is ``1``.927928/929 Divide two numbers. The return value will be a floating point number.930 ``{{ 1 / 2 }}`` is ``{{ 0.5 }}``.
2 unmodified lines
933 Divide two numbers and return the truncated integer result.934 ``{{ 20 / 7 }}`` is ``2``.935936%937 Calculate the remainder of an integer division between the left and right938 operand. ``{{ 11 % 7 }}`` is ``4``.937 Calculate the remainder of an integer division. ``{{ 11 % 7 }}`` is ``4``.938939\*940 Multiply the left operand with the right one. ``{{ 2 * 2 }}`` would942 return ``4``. This can also be used to repeat string multiple times.941 return ``4``. This can also be used to repeat a string multiple times.942 ``{{ '=' * 80 }}`` would print a bar of 80 equal signs.943944\**945 Raise the left operand to the power of the right operand. ``{{ 2**3 }}``
1 unmodified line
947948Logic949~~~~~950952For `if` statements / `for` filtering or `if` expressions it can be useful to953combine group multiple expressions:951For `if` statements, `for` filtering or `if` expressions it can be useful to952combine multiple expressions:953954and955 Return true if the left and the right operand is true.956
37 unmodified lines
994 ``'John'``) ``Hello John!``.995996()997 Call a callable: ``{{ post.render() }}``. Inside of the parentheses you999 can use arguments and keyword arguments like in python:998 can use positional arguments and keyword arguments like in python:999 ``{{ post.render(user, full=true) }}``.10001001. / []1002 Get an attribute of an object. (See :ref:`variables`)
80 unmodified lines
1083 works. Unlike `loop.cycle` however you can use this cycler outside of1084 loops or over multiple loops.10851086 This is for example very useful if you want to show a list of folders and1088 files, with the folders on top, but both in the same list with alteranting1087 files, with the folders on top, but both in the same list with alternating1088 row colors.10891090 The following example shows how `cycler` can be used::1091
147 unmodified lines
modified:jinja2/environment.py+2 added-2 removed
654 unmodified lines
655 vars, shared, self.globals, locals)656657 def make_module(self, vars=None, shared=False, locals=None):658 """This method works like the :attr:`module` attribute when called659 without arguments but it will evaluate the template every call660 rather then caching the template. It's also possible to provide659 without arguments but it will evaluate the template on every call660 rather than caching it. It's also possible to provide661 a dict which is then used as context. The arguments are the same662 as for the :meth:`new_context` method.663 """664 return TemplateModule(self, self.new_context(vars, shared, locals))
176 unmodified lines
modified:jinja2/filters.py+4 added-4 removed
283 unmodified lines
284285def do_filesizeformat(value, binary=False):286 """Format the value like a 'human-readable' file size (i.e. 13 KB,287 4.1 MB, 102 bytes, etc). Per default decimal prefixes are used (mega,288 giga etc.), if the second parameter is set to `True` the binary289 prefixes are (mebi, gibi).288 giga, etc.), if the second parameter is set to `True` the binary289 prefixes are used (mebi, gibi).290 """291 bytes = float(value)292 base = binary and 1024 or 1000293 middle = binary and 'i' or ''
157 unmodified lines
451452def do_slice(value, slices, fill_with=None):453 """Slice an iterator and return a list of lists containing454 those items. Useful if you want to create a div containing455 three div tags that represent columns:455 three ul tags that represent columns:456457 .. sourcecode:: html+jinja458459 <div class="columwrapper">
37 unmodified lines
497 <table>498 {%- for row in items|batch(3, '&nbsp;') %}499 <tr>500 {%- for column in row %}501 <tr>{{ column }}</td>501 <td>{{ column }}</td>502 {%- endfor %}503 </tr>504 {%- endfor %}505 </table>
210 unmodified lines