lino.utils.jinja¶
This defines the Counter
class, a utility used in Jinja
templates to generate self-incrementing counters for sections,
subsections and any other sequences.
Installation¶
You can add the Counter class either to your local context or to the global namespace.
>>> from jinja2 import Environment
>>> from lino.utils.jinja import Counter
>>> env = Environment()
>>> env.globals.update(Counter=Counter)
Basic usage in a template¶
Using the Counter in your template is easy: You instantiate a
template variable of type Counter
, and then call that counter
each time you want a new number. For example:
>>> s = """
... {%- set art = Counter() -%}
... Easy as {{art()}}, {{art()}} and {{art()}}!
... """
Here is how this template will render :
>>> print(env.from_string(s).render())
Easy as 1, 2 and 3!
Counter parameters¶
When defining your counter, you can set optional parameters.
>>> s = """
... {%- set art = Counter(start=17, step=2) -%}
... A funny counter: {{art()}}, {{art()}} and {{art()}}!
... """
>>> print(env.from_string(s).render())
A funny counter: 19, 21 and 23!
Resetting a counter¶
When calling your counter, you can pass optional parameters. One of them is value, which you can use to restart numbering, or to start numbering at some arbitrary place:
>>> s = """
... {%- set art = Counter() -%}
... First use: {{art()}}, {{art()}} and {{art()}}
... Reset: {{art(value=1)}}, {{art()}} and {{art()}}
... Arbitrary start: {{art(value=10)}}, {{art()}} and {{art()}}
... """
>>> print(env.from_string(s).render())
First use: 1, 2 and 3
Reset: 1, 2 and 3
Arbitrary start: 10, 11 and 12
Nested counters¶
Counters can have another counter as parent. When a parent increases, all children are automatically reset to their start value.
>>> s = """
... {%- set art = Counter() -%}
... {%- set par = Counter(art) -%}
... = Article {{art()}}
... == # {{par()}}
... == # {{par()}}
... = Article {{art()}}
... == # {{par()}}
... == # {{par()}}
... == # {{par()}}
... Article {{art()}}.
... == # {{par()}}
... """
>>> print(env.from_string(s).render())
= Article 1
== # 1
== # 2
= Article 2
== # 1
== # 2
== # 3
Article 3.
== # 1
(This module's source code is available here.)
Functions
|
Classes
|
Represents a counter. |
-
class
lino.utils.jinja.
Counter
(parent=None, start=0, step=1)¶ Bases:
object
Represents a counter. Usage see