Bundled jinja filters.
- :copyright: 2008 by Armin Ronacher, Christoph Hack.
+ :copyright: (c) 2010 by the Jinja Team.
:license: BSD, see LICENSE for more details.
"""
import re
from jinja2.exceptions import FilterArgumentError, SecurityError
-_word_re = re.compile(r'\w+')
+_word_re = re.compile(r'\w+(?u)')
def contextfilter(f):
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 %}
.. sourcecode:: jinja
{{ 42.55|round }}
- -> 43
+ -> 43.0
{{ 42.55|round(1, 'floor') }}
-> 42.5
+
+ Note that even if rounded to 0 precision, a float is returned. If
+ you need a real integer, pipe it through `int`:
+
+ .. sourcecode:: jinja
+
+ {{ 42.55|round|int }}
+ -> 43
"""
if not method in ('common', 'ceil', 'floor'):
raise FilterArgumentError('method must be common, ceil or floor')