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
Provenance
- Commit
709f6e5- Tree
a7ed435- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
157531b
3 files changed+211 added−5 removed
modified:docs/templates.rst+200 added-1 removed
46 unmodified lines47 There are two kinds of delimiers. ``{% ... %}`` and ``{{ ... }}``. The first48 one is used to execute statements such as for-loops or assign values, the49 latter prints the result of the expression to the template.50 51+.. _variables:52 53 Variables54 ---------55 19 unmodified lines75 value. What you can do with that kind of value depends on the application76 configuration, the default behavior is that it evaluates to an empty string77 if printed and that you can iterate over it, but every other operation fails.78 79+.. _filters:80 81 Filters82 -------83 8 unmodified lines92 by spaces: ``{{ list|join(', ') }}``.93 94 The :ref:`builtin-filters` below describes all the builtin filters.95 96+.. _tests:97 98 Tests99 -----100 226 unmodified lines327 328 Inside of a for loop block you can access some special variables:329 330 +-----------------------+---------------------------------------------------+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 lines358 {% for row in rows %}359 <li class="{{ loop.cycle('odd', 'even') }}">{{ row }}</li>360 {% endfor %}361 362+.. _loop-filtering:363+364 Unlike in Python it's not possible to `break` or `continue` in a loop. You365 can however filter the sequence during iteration which allows you to skip366 items. The following example skips all the users which are hidden:367 18 unmodified lines386 {% else %}387 <li><em>no users found</em></li>388 {% endif %}389 </ul>383- 390 391+392+If393+~~394+395+The `if` statement in Jinja is comparable with the if statements of Python.396+In the simplest form you can use it to test if a variable is defined, not397+empty or not false:398+399+.. sourcecode:: html+jinja400+401+ {% if users %}402+ <ul>403+ {% for user in users %}404+ <li>{{ user.username|e }}</li>405+ {% endfor %}406+ </ul>407+ {% endif %}408+409+For multiple branches `elif` and `else` can be used like in Python. You can410+use more complex :ref:`expressions` there too.411+412+.. sourcecode:: html+jinja413+414+ {% 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 %}421+422+If can also be used as :ref:`inline expression <if-expression>` and for423+:ref:`loop filtering <loop-filtering>`.424+425+426+.. _expressions:427+428+Expressions429+-----------430+431+Jinja allows basic expressions everywhere. These work very similar to regular432+Python and even if you're not working with Python you should feel comfortable433+with it.434+435+Literals436+~~~~~~~~437+438+The simplest form of expressions are literals. Literals are representations439+for Python objects such as strings and numbers. The following literals exist:440+441+``"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).446+447+``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.452+453+``['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.457+458+ .. sourcecode:: html+jinja459+460+ <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>466+467+``('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.472+473+``{'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.478+479+``true`` and ``false``480+ true is always true and false is always false. Keep in mind that those481+ literals are lowercase!482+483+Math484+~~~~485+486+Jinja allows you to calculate with values. This is rarely useful in templates487+but exists for completeness sake. The following operators are supported:488+489+``+``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``.494+495+``-``496+ Substract two numbers from each other. ``{{ 3 - 2 }}`` is ``1``.497+498+``/``499+ Divide two numbers. The return value will be a floating point number.500+ ``{{ 1 / 2 }}`` is ``{{ 0.5 }}``.501+502+``//``503+ Divide two numbers and return the truncated integer result.504+ ``{{ 20 / 7 }}`` is ``2``.505+506+``%``507+ Calculate the remainder of an integer division between the left and right508+ operand. ``{{ 11 % 7 }}`` is ``4``.509+510+``*``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.514+515+``**``516+ Raise the left operand to the power of the right operand. ``{{ 2**3 }}``517+ would return ``8``.518+519+Logic520+~~~~~521+522+For `if` statements / `for` filtering or `if` expressions it can be useful to523+combine group multiple expressions:524+525+``and``526+ Return true if the left and the right operand is true.527+528+``or``529+ Return true if the left or the right operand is true.530+531+``not``532+ negate a statement (see below).533+534+``()`` (group)535+ group an expression.536+537+Note that there is no support for any bit operations or something similar.538+539+- 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).``543+544+545+Other Operators546+~~~~~~~~~~~~~~~547+548+The following operators are very useful but don't fit into any of the other549+two categories:550+551+``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.555+556+``is``557+ Performs a :ref:`tests <test>`.558+559+``|``560+ Applies a :ref:`filters <filter>`.561+562+``~``563+ Converts all operands into strings and concatenates them.564+ ``{{ "Hello " ~ name ~ "!" }}`` would return (assuming `name` is565+ ``'John'``) ``Hello John!``.566+567+``()`` (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) }}``.571+572+``.`` / ``[]``573+ Get an attribute of an object. (See :ref:`variables`)574+575+576+.. _if-expression:577+578+If Expression579+~~~~~~~~~~~~~580+581+blah582+583+584 .. _builtin-filters:585 586 List of Builtin Filters587 -----------------------10 unmodified linesmodified:jinja2/compiler.py+3 added-1 removed
555 unmodified lines556 557 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)563 564 # do we have an extends tag at all? If not, we can save some551 unmodified lines1116 visit_Not = uaop('not ')1117 del binop, uaop1118 1119 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 linesmodified:jinja2/utils.py+8 added-3 removed
245 unmodified lines246 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)254+255+ # 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_wrapper260 261 262 class _MarkupEscapeHelper(object):164 unmodified lines