Skip to content
Myr

python2.4 compatibility and doc updates

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 157531b · Browse files · 3 files+211 added−5 removed
This commit

State

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

Diff stats

3 files+211 added−5 removed

  • other:+211 added−5 removed

3 files+211 added−5 removed

modified:docs/templates.rst+200 added-1 removed
46 unmodified lines
47There are two kinds of delimiers. ``{% ... %}`` and ``{{ ... }}``. The first48one is used to execute statements such as for-loops or assign values, the49latter prints the result of the expression to the template.5051.. _variables:5253Variables54---------55
19 unmodified lines
75value. What you can do with that kind of value depends on the application76configuration, the default behavior is that it evaluates to an empty string77if printed and that you can iterate over it, but every other operation fails.7879.. _filters:8081Filters82-------83
8 unmodified lines
92by spaces: ``{{ list|join(', ') }}``.9394The :ref:`builtin-filters` below describes all the builtin filters.9596.. _tests:9798Tests99-----100
226 unmodified lines
327328Inside of a for loop block you can access some special variables:329330+-----------------------+---------------------------------------------------+331| Variable | Description |332+=======================+===================================================+333| `loop.index` | The current iteration of the loop. (1 indexed) |334+-----------------------+---------------------------------------------------+335| `loop.index0` | The current iteration of the loop. (0 indexed) |336+-----------------------+---------------------------------------------------+
21 unmodified lines
358 {% for row in rows %}359 <li class="{{ loop.cycle('odd', 'even') }}">{{ row }}</li>360 {% endfor %}361362.. _loop-filtering:363364Unlike in Python it's not possible to `break` or `continue` in a loop. You365can however filter the sequence during iteration which allows you to skip366items. The following example skips all the users which are hidden:367
18 unmodified lines
386 {% else %}387 <li><em>no users found</em></li>388 {% endif %}389 </ul>383 390391392If393~~394395The `if` statement in Jinja is comparable with the if statements of Python.396In the simplest form you can use it to test if a variable is defined, not397empty or not false:398399.. sourcecode:: html+jinja400401 {% if users %}402 <ul>403 {% for user in users %}404 <li>{{ user.username|e }}</li>405 {% endfor %}406 </ul>407 {% endif %}408409For multiple branches `elif` and `else` can be used like in Python. You can410use more complex :ref:`expressions` there too.411412.. sourcecode:: html+jinja413414 {% if kenny.sick %}415 Kenny is sick.416 {% elif kenny.dead %}417 You killed Kenny! You bastard!!!418 {% else %}419 Kenny looks okay --- so far420 {% endif %}421422If can also be used as :ref:`inline expression <if-expression>` and for423:ref:`loop filtering <loop-filtering>`.424425426.. _expressions:427428Expressions429-----------430431Jinja allows basic expressions everywhere. These work very similar to regular432Python and even if you're not working with Python you should feel comfortable433with it.434435Literals436~~~~~~~~437438The simplest form of expressions are literals. Literals are representations439for Python objects such as strings and numbers. The following literals exist:440441``"Hello World"``442 Everything between two double or single quotes is a string. They are443 useful whenever you need a string in the template (for example as444 arguments to function calls, filters or just to extend or include a445 template).446447``42`` / ``42.23``448 Integers and floating point numbers are created by just writing the449 number down. If a dot is present the number is a float, otherwise an450 integer. Keep in mind that for Python ``42`` and ``42.0`` is something451 different.452453``['list', 'of', 'objects']``454 Everything between two brackets is a list. Lists are useful to store455 sequential data in or to iterate over them. For example you can easily456 create a list of links using lists and tuples with a for loop.457458 .. sourcecode:: html+jinja459460 <ul>461 {% for href, caption in [('index.html', 'Index'), ('about.html', 'About'),462 ('downloads.html', 'Downloads')] %}463 <li><a href="{{ href }}">{{ caption }}</a></li>464 {% endfor %}465 </ul>466467``('tuple', 'of', 'values')``468 Tuples are like lists, just that you can't modify them. If the tuple469 only has one item you have to end it with a comma. Tuples are usually470 used to represent items of two or more elements. See the example above471 for more details.472473``{'dict': 'of', 'keys': 'and', 'value': 'pairs'}``474 A dict in Python is a structure that combines keys and values. Keys must475 be unique and always have exactly one value. Dicts are rarely used in476 templates, they are useful in some rare cases such as the :func:`xmlattr`477 filter.478479``true`` and ``false``480 true is always true and false is always false. Keep in mind that those481 literals are lowercase!482483Math484~~~~485486Jinja allows you to calculate with values. This is rarely useful in templates487but exists for completeness sake. The following operators are supported:488489``+``490 Adds two objects with each other. Usually numbers but if both objects are491 strings or lists you can concatenate them this way. This however is not492 the preferred way to concatenate strings! For string concatenation have493 a look at the ``~`` operator. ``{{ 1 + 1 }}`` is ``2``.494495``-``496 Substract two numbers from each other. ``{{ 3 - 2 }}`` is ``1``.497498``/``499 Divide two numbers. The return value will be a floating point number.500 ``{{ 1 / 2 }}`` is ``{{ 0.5 }}``.501502``//``503 Divide two numbers and return the truncated integer result.504 ``{{ 20 / 7 }}`` is ``2``.505506``%``507 Calculate the remainder of an integer division between the left and right508 operand. ``{{ 11 % 7 }}`` is ``4``.509510``*``511 Multiply the left operand with the right one. ``{{ 2 * 2 }}`` would512 return ``4``. This can also be used to repeat string multiple times.513 ``{{ '=' * 80 }}`` would print a bar of 80 equal signs.514515``**``516 Raise the left operand to the power of the right operand. ``{{ 2**3 }}``517 would return ``8``.518519Logic520~~~~~521522For `if` statements / `for` filtering or `if` expressions it can be useful to523combine group multiple expressions:524525``and``526 Return true if the left and the right operand is true.527528``or``529 Return true if the left or the right operand is true.530531``not``532 negate a statement (see below).533534``()`` (group)535 group an expression.536537Note that there is no support for any bit operations or something similar.538539- special note regarding ``not``: The ``is`` and ``in`` operators support540 negation using an infix notation too: ``foo is not bar`` and541 ``foo not in bar`` instead of ``not foo is bar`` and ``not foo in bar``.542 All other expressions require a prefix notation: ``not (foo and bar).``543544545Other Operators546~~~~~~~~~~~~~~~547548The following operators are very useful but don't fit into any of the other549two categories:550551``in``552 Perform sequence / mapping containment test. Returns true if the left553 operand is contained in the right. ``{{ 1 in [1, 2, 3] }}`` would for554 example return true.555556``is``557 Performs a :ref:`tests <test>`.558559``|``560 Applies a :ref:`filters <filter>`.561562``~``563 Converts all operands into strings and concatenates them.564 ``{{ "Hello " ~ name ~ "!" }}`` would return (assuming `name` is565 ``'John'``) ``Hello John!``.566567``()`` (call)568 Call a callable: ``{{ post.render() }}``. Inside of the parentheses you569 can use arguments and keyword arguments like in python:570 ``{{ post.render(user, full=true) }}``.571572``.`` / ``[]``573 Get an attribute of an object. (See :ref:`variables`)574575576.. _if-expression:577578If Expression579~~~~~~~~~~~~~580581blah582583584.. _builtin-filters:585586List of Builtin Filters587-----------------------
10 unmodified lines
modified:jinja2/compiler.py+3 added-1 removed
555 unmodified lines
556557 def visit_Template(self, node, frame=None):558 assert frame is None, 'no root frame allowed'559 from jinja2.runtime import __all__ as exported560 self.writeline('from __future__ import division')561 self.writeline('from jinja2.runtime import ' + ', '.join(exported))562 self.writeline('name = %r' % self.name)563564 # do we have an extends tag at all? If not, we can save some
551 unmodified lines
1116 visit_Not = uaop('not ')1117 del binop, uaop11181119 def visit_Concat(self, node, frame):1119 self.write('join((')1120 self.write('%s((' % self.environment.autoescape and1121 'markup_join' or 'unicode_join')1122 for arg in node.nodes:1123 self.visit(arg, frame)1124 self.write(', ')1125 self.write('))')
98 unmodified lines
modified:jinja2/utils.py+8 added-3 removed
245 unmodified lines
246 func.__doc__ = orig.__doc__247 return func248 for method in '__getitem__', '__getslice__', 'capitalize', \249 'title', 'lower', 'upper', 'replace', 'ljust', \250 'rjust', 'lstrip', 'rstrip', 'partition', 'center', \251 'strip', 'translate', 'expandtabs', 'rpartition', \252 'swapcase', 'zfill':250 'rjust', 'lstrip', 'rstrip', 'center', 'strip', \251 'translate', 'expandtabs', 'rpartition', 'swapcase', \252 'zfill':253 locals()[method] = make_wrapper(method)254255 # new in python 2.5256 if hasattr(unicode, 'partition'):257 locals().update(partition=make_wrapper('partition'),258 rpartition=make_wrapper('rpartition'))259 del method, make_wrapper260261262class _MarkupEscapeHelper(object):
164 unmodified lines