List of Global Functions

The following functions are available in the global scope by default:

  • range([start, ]stop[, step])
  • Return a list containing an arithmetic progression of integers.range(i, j) returns [i, i+1, i+2, …, j-1];start (!) defaults to 0.When step is given, it specifies the increment (or decrement).For example, range(4) and range(0, 4, 1) return [0, 1, 2, 3].The end point is omitted!These are exactly the valid indices for a list of 4 elements.

This is useful to repeat a template block multiple times, e.g.to fill a list. Imagine you have 7 users in the list but you want torender three empty items to enforce a height with CSS:

  1. <ul>
  2. {% for user in users %}
  3. <li>{{ user.username }}</li>
  4. {% endfor %}
  5. {% for number in range(10 - users|count) %}
  6. <li class="empty"><span>...</span></li>
  7. {% endfor %}
  8. </ul>
  • lipsum(n=5, html=True, min=20, max=100)
  • Generates some lorem ipsum for the template. By default, five paragraphsof HTML are generated with each paragraph between 20 and 100 words.If html is False, regular text is returned. This is useful to generate simplecontents for layout testing.
  • dict(**items)
  • A convenient alternative to dict literals. {'foo': 'bar'} is the sameas dict(foo='bar').
  • class cycler(*items)
  • The cycler allows you to cycle among values similar to how loop.cycle_works. Unlike _loop.cycle, you can use this cycler outside ofloops or over multiple loops.

This can be very useful if you want to show a list of folders andfiles with the folders on top but both in the same list with alternatingrow colors.

The following example shows how cycler can be used:

  1. {% set row_class = cycler('odd', 'even') %}
  2. <ul class="browser">
  3. {% for folder in folders %}
  4. <li class="folder {{ row_class.next() }}">{{ folder|e }}</li>
  5. {% endfor %}
  6. {% for filename in files %}
  7. <li class="file {{ row_class.next() }}">{{ filename|e }}</li>
  8. {% endfor %}
  9. </ul>

A cycler has the following attributes and methods:

  • reset()
  • Resets the cycle to the first item.

  • next()

  • Goes one item ahead and returns the then-current item.

  • current

  • Returns the current item.

Changelog

New in version 2.1.

  • class joiner(sep=', ')
  • A tiny helper that can be used to “join” multiple sections. A joiner ispassed a string and will return that string every time it’s called, exceptthe first time (in which case it returns an empty string). You canuse this to join things:
  1. {% set pipe = joiner("|") %}
  2. {% if categories %} {{ pipe() }}
  3. Categories: {{ categories|join(", ") }}
  4. {% endif %}
  5. {% if author %} {{ pipe() }}
  6. Author: {{ author() }}
  7. {% endif %}
  8. {% if can_edit %} {{ pipe() }}
  9. <a href="?action=edit">Edit</a>
  10. {% endif %}

Changelog

New in version 2.1.

  • class namespace()
  • Creates a new container that allows attribute assignment using the{% set %} tag:
  1. {% set ns = namespace() %}
  2. {% set ns.foo = 'bar' %}

The main purpose of this is to allow carrying a value from within a loopbody to an outer scope. Initial values can be provided as a dict, askeyword arguments, or both (same behavior as Python’s dict constructor):

  1. {% set ns = namespace(found=false) %}
  2. {% for item in items %}
  3. {% if item.check_something() %}
  4. {% set ns.found = true %}
  5. {% endif %}
  6. * {{ item.title }}
  7. {% endfor %}
  8. Found item having something: {{ ns.found }}

New in version 2.10.