Skip to content
Myr

format expression docs

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

authored by David Lord · committed by David Lord · parent d2bf582 · Browse files · 1 file+37 added−37 removed
This commit

State

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

Diff stats

1 file+37 added−37 removed

  • other:+37 added−37 removed

1 file+37 added−37 removed

modified:docs/templates.rst+37 added-37 removed
1172 unmodified lines
11731174The simplest form of expressions are literals. Literals are representations1175for Python objects such as strings and numbers. The following literals exist:11761177"Hello World":1177``"Hello World"``1178 Everything between two double or single quotes is a string. They are1179 useful whenever you need a string in the template (e.g. as1180 arguments to function calls and filters, or just to extend or include a1181 template).1182118342 / 123_456:1183``42`` / ``123_456``1184 Integers are whole numbers without a decimal part. The '_' character1185 can be used to separate groups for legibility.1186118742.23 / 42.1e2 / 123_456.789:1187``42.23`` / ``42.1e2`` / ``123_456.789``1188 Floating point numbers can be written using a '.' as a decimal mark.1189 They can also be written in scientific notation with an upper or1190 lower case 'e' to indicate the exponent part. The '_' character can1191 be used to separate groups for legibility, but cannot be used in the1192 exponent part.11931194['list', 'of', 'objects']:1194``['list', 'of', 'objects']``1195 Everything between two brackets is a list. Lists are useful for storing1196 sequential data to be iterated over. For example, you can easily1197 create a list of links using lists and tuples for (and with) a for loop::1198
3 unmodified lines
1202 <li><a href="{{ href }}">{{ caption }}</a></li>1203 {% endfor %}1204 </ul>12051206('tuple', 'of', 'values'):1206``('tuple', 'of', 'values')``1207 Tuples are like lists that cannot be modified ("immutable"). If a tuple1208 only has one item, it must be followed by a comma (``('1-tuple',)``).1209 Tuples are usually used to represent items of two or more elements.1210 See the list example above for more details.12111212{'dict': 'of', 'key': 'and', 'value': 'pairs'}:1212``{'dict': 'of', 'key': 'and', 'value': 'pairs'}``1213 A dict in Python is a structure that combines keys and values. Keys must1214 be unique and always have exactly one value. Dicts are rarely used in1215 templates; they are useful in some rare cases such as the :func:`xmlattr`1216 filter.12171218true / false:1219 true is always true and false is always false.1218``true`` / ``false``1219 ``true`` is always true and ``false`` is always false.12201221.. admonition:: Note12221223 The special constants `true`, `false`, and `none` are indeed lowercase.
9 unmodified lines
12331234Jinja allows you to calculate with values. This is rarely useful in templates1235but exists for completeness' sake. The following operators are supported:12361237\+1237``+``1238 Adds two objects together. Usually the objects are numbers, but if both are1239 strings or lists, you can concatenate them this way. This, however, is not1240 the preferred way to concatenate strings! For string concatenation, have1241 a look-see at the ``~`` operator. ``{{ 1 + 1 }}`` is ``2``.12421243\-1243``-``1244 Subtract the second number from the first one. ``{{ 3 - 2 }}`` is ``1``.12451246/1246``/``1247 Divide two numbers. The return value will be a floating point number.1248 ``{{ 1 / 2 }}`` is ``{{ 0.5 }}``.12491250//1250``//``1251 Divide two numbers and return the truncated integer result.1252 ``{{ 20 // 7 }}`` is ``2``.12531254%1254``%``1255 Calculate the remainder of an integer division. ``{{ 11 % 7 }}`` is ``4``.12561257\*1257``*``1258 Multiply the left operand with the right one. ``{{ 2 * 2 }}`` would1259 return ``4``. This can also be used to repeat a string multiple times.1260 ``{{ '=' * 80 }}`` would print a bar of 80 equal signs.12611262\**1262``**``1263 Raise the left operand to the power of the right operand. ``{{ 2**3 }}``1264 would return ``8``.12651266Comparisons1267~~~~~~~~~~~12681269==1269``==``1270 Compares two objects for equality.12711272!=1272``!=``1273 Compares two objects for inequality.12741275>1276 `true` if the left hand side is greater than the right hand side.1275``>``1276 ``true`` if the left hand side is greater than the right hand side.12771278>=1279 `true` if the left hand side is greater or equal to the right hand side.1278``>=``1279 ``true`` if the left hand side is greater or equal to the right hand side.12801281<1282 `true` if the left hand side is lower than the right hand side.1281``<``1282 ``true`` if the left hand side is lower than the right hand side.12831284<=1285 `true` if the left hand side is lower or equal to the right hand side.1284``<=``1285 ``true`` if the left hand side is lower or equal to the right hand side.12861287Logic1288~~~~~12891290For `if` statements, `for` filtering, and `if` expressions, it can be useful to1290For ``if`` statements, ``for`` filtering, and ``if`` expressions, it can be useful to1291combine multiple expressions:12921293and1293``and``1294 Return true if the left and the right operand are true.12951296or1296``or``1297 Return true if the left or the right operand are true.12981299not1299``not``1300 negate a statement (see below).13011302(expr)1303 group an expression.1302``(expr)``1303 Parentheses group an expression.13041305.. admonition:: Note13061307 The ``is`` and ``in`` operators support negation using an infix notation,
7 unmodified lines
13151316The following operators are very useful but don't fit into any of the other1317two categories:13181319in1319``in``1320 Perform a sequence / mapping containment test. Returns true if the left1321 operand is contained in the right. ``{{ 1 in [1, 2, 3] }}`` would, for1322 example, return true.13231324is1324``is``1325 Performs a :ref:`test <tests>`.13261327\|1327``|``1328 Applies a :ref:`filter <filters>`.13291330~1330``~``1331 Converts all operands into strings and concatenates them.13321333 ``{{ "Hello " ~ name ~ "!" }}`` would return (assuming `name` is set1334 to ``'John'``) ``Hello John!``.13351336()1336``()``1337 Call a callable: ``{{ post.render() }}``. Inside of the parentheses you1338 can use positional arguments and keyword arguments like in Python:13391340 ``{{ post.render(user, full=true) }}``.13411342. / []1342``.`` / ``[]``1343 Get an attribute of an object. (See :ref:`variables`)134413451346.. _if-expression:
433 unmodified lines