gaetk2.jinja_filters module - template filters¶
These filters do a lot of formatting and conversion. They are Build with German localisation and HTML in mind to extend Jinja’s own filters
Use them like this in your templates:
{{ body|markdown }}
<div class="{{ obj.designator|cssencode }}">
If you use gaetk2.handlers these filters are made available automatically.
If not youncan include them via register_custom_filters()
.
Spacing Issues¶
We surrently use u’u202F’ NARROW NO-BREAK SPACE U+202F to separate numbers. Unfortunately this is missing in most fonts and not well supported in browsers.
Services provided¶
Access Control¶
These Access Control filters are somewhat more involved because they need the cooperation of the rest of gaetk2. They are meant to show certain parts of a template only to certain users.
See gaetk2.handler.base.is_staff()
and
gaetk.models.gaetk_Credential
for further Reference.
onlystaff()
- display content only if the currently logged in useris_staff()
.
Encoding¶
Ensure a variable is a valid value for CSS, URL, XML attribute.
cssencode()
- urlencode - legacy, now part of Jinja >= 2.7.
Date-Formatting¶
dateformat()
- formats adate
object.datetimeformat()
- formats adatetime
object.tertial()
- outputs a tertial (opposed to quater).
Number-Formating¶
User-Readable Number formatting. All of these assume you are outputting HTML.
nicenum()
- seperates thousands by spacesintword()
- 1200000 becomes ‘1.2 Mio’ andeuroword()
- divides cents by 100 and returns anintword()
eurocent()
- divides cants by 100 and returns annicenum()
.g2kg()
- convert to a human readable weigth measurment in g/kg/t. See also filesizeformat in jinja2.percent()
- aNone
tollerant"%.0f"
iban()
- Format An International Banking Code.
Text-Formatting¶
Many of these functions are most relevant for settings where you
have <pre>>
or want to reach a similar effect in HTML.
markdown()
- convert Markdown to HTML.nl2br()
- basically get the output of<pre>
without using<pre>
.left_justify()
right_justify()
Boolean-Formatting (and None)¶
Displaying Booleans with the ability to distinguish between (True, False, None)
.
GAETK1 Compability¶
datetime
has been renamed to datetimeformat.to_json
is gone, use tojson in jinja2 2.9.urlencode
is gone, use urlencode in jinja2 2.7. the urlencode provided by jinja has much more features than we had.attrencode
is gone, use xmlattr in jinja2 2.9.- generally we now return only Unicode Plain Text, no HTML.
nicenum
,eurocent``and ``g2kg
are changed by that. - the urlencode provided by jinja2
Module contents¶
Display content only if the current logged in user has a specific permission.
-
gaetk2.jinja_filters.
onlystaff
(ctx, value, tag=None)[source]¶ Display content only if the current logged in user is_staff().
This tag generatyes HTML. If you don’t wan’t HTML use this construct:
{% if is_staff() %}Internal Info{% endif %}
The Tag encloses content in a
<span>
or<div>
depending on it’s contents:{{ "bla"|onlystaff }} <!-- is rendered to: --> <span class="gaetk_onlystaff">bla</span> {% filter onlystaff %} <form ...></form> {% endfilter %} <!-- is rendered to: --> <div class="gaetk_onlystaff"> <form ...></form> </div> {% filter onlystaff %} <i>test text</i> {% endfilter %} <!-- is rendered to: --> <span class="gaetk_onlystaff"><i>test text</i></span>
If you not happy with how the filter chooses between
<span>
and<div>
you can provide a tag to be used. Or you can provide empty data to avoid all markup:{% filter onlystaff('p') %} <i>test text</i> {% endfilter %} <!-- is rendered to: --> <p class="gaetk_onlystaff">bla</p> {% filter onlystaff('') %} foo {% endfilter %} <!-- is rendered to: --> foo
Automatic detection does not work perfectly within tables. Your milage may vary.
If the user is not staff an empty tag is generated:
{% filter onlystaff %} supersecret {% endfilter %} <!-- is rendered to: --> <span class="gaetk_onlystaff-denied"><!-- !is_staff() --></span> {% filter onlystaff('') %} supersecret {% endfilter %} <!-- is rendered to: (nothing) -->
-
gaetk2.jinja_filters.
cssencode
(value)[source]¶ Makes a string valid as an CSS class name.
This ensured only valid characters are used and the class name starts with an character. This is enforced by prefixing CSS if the string does not start with an character:
<div class="{{ 5711|cssencode }} {{ 'root beer'|cssencode }}"> >>> '<div class="CSS5711 root-beer">'
-
gaetk2.jinja_filters.
dateformat
(value, formatstring='%Y-%m-%d', nonchar=u'')[source]¶ Formates a date.
Tries to convert the given
value
to adate
object and then formats it according toformatstring
:{{ date.today()|dateformat }} {{ "20171224"|dateformat('%Y-%W') }}
-
gaetk2.jinja_filters.
datetimeformat
(value, formatstring='%Y-%m-%d %H:%M', nonchar=u'')[source]¶ Formates a datetime.
Tries to convert the given
value
to adatetime
object and then formats it according toformatstring
:{{ datetime.now()|datetimeformat }} {{ "20171224T235959"|datetimeformat('%H:%M') }}
-
gaetk2.jinja_filters.
tertial
(value, nonchar=u'\u2400')[source]¶ Change a Date oder Datetime-Objekt into a Tertial-String.
Tertials are third-years as opposed to quater years:
{{ "20170101"|tertial }} {{ "20170606"|tertial }} {{ "20171224"|tertial }} >>> "2017-A" "2017-B" "2017-C"
-
gaetk2.jinja_filters.
nicenum
(value, spacer=u'\u202f', nonchar=u'\u2400')[source]¶ Format the given number with spacer as delimiter, e.g. 1 234 456.
Default spacer is NARROW NO-BREAK SPACE U+202F. Probably style=”white-space:nowrap; word-spacing:0.5em;” would be an CSS based alternative.
-
gaetk2.jinja_filters.
intword
(value, nonchar=u'\u2400')[source]¶ Converts a large integer to a friendly text representation.
Works best for numbers over 1 million. For example, 1000000 becomes ‘1.0 Mio’, 1200000 becomes ‘1.2 Mio’ and ‘1200000000’ becomes ‘1200 Mio’.
-
gaetk2.jinja_filters.
eurocent
(value, spacer=u'\u202f', decimalplaces=2, nonchar=u'\u2400')[source]¶ Format the given cents as Euro with spacer as delimiter, e.g. ‘1 234 456.23’.
Obviously works also with US$ and other 100-based. currencies.
This is like :func:nicenum. Use
decimalplaces=0
to cut of cents, but even better use :func:euroword.Default spacer is NARROW NO-BREAK SPACE U+202F. Probably style=”white-space:nowrap; word-spacing:0.5em;” would be an CSS based alternative.
-
gaetk2.jinja_filters.
euroword
(value, plain=False, nonchar=u'\u2400')[source]¶ Fomat Cents as pretty Euros.
-
gaetk2.jinja_filters.
g2kg
(value, spacer=u'\u202f', nonchar=u'\u2400')[source]¶ Wandelt meist g in kg um, aber auch in andere Einheiten.
-
gaetk2.jinja_filters.
iban
(value, spacer=u'\u202f', nonchar=u'\u2400')[source]¶ Format the given string like an IBAN Account Number.
Default spacer is NARROW NO-BREAK SPACE U+202F.
Eg:
{{ "DE77123413500000567844"|iban }} DE77 1234 1350 0000 5678 44
-
gaetk2.jinja_filters.
markdown
(value)[source]¶ Renders a string as Markdown.
- Syntax:
- {{ value|markdown }}
We are using markdown2 to do the rendering.
-
gaetk2.jinja_filters.
left_justify
(value, width)[source]¶ Prefix the given string with spaces until it is width characters long.
-
gaetk2.jinja_filters.
right_justify
(value, width)[source]¶ Postfix the given string with spaces until it is width characters long.
-
gaetk2.jinja_filters.
yesno
(value, answers=u'yes, no, maybe')[source]¶ Output a text based on Falsyness, Trueyness and
is None
.Example:
{{ value|yesno:"yeah,nope,maybe" }}
.
-
gaetk2.jinja_filters.
onoff
(value)[source]¶ Display Boolean as Font Awesome Icon Icon darstellen.
We use Font Awesome toogle-on and toogle-of to indicate state.
-
gaetk2.jinja_filters.
none
(value, nonchar=u'')[source]¶ Converts
None
to''
.Similar to
|default('', true)
in jinja2 but more explicit.
-
gaetk2.jinja_filters.
plural
(value, singular_str, plural_str)[source]¶ Return value with singular or plural form.
{{ l|length|plural('Items', 'Items') }}