Reputation: 90211
I'm using Jinja2, and I'm trying to create a couple tags that work together, such that if I have a template that looks something like this:
{{ my_summary() }}
... arbitrary HTML ...
{{ my_values('Tom', 'Dick', 'Harry') }}
... arbitrary HTML ...
{{ my_values('Fred', 'Barney') }}
I'd end up with the following:
This page includes information about <b>Tom</b>, <b>Dick</b>, <b>Harry</b>, <b>Fred</b>, and <b>Barney</b>.
... arbitrary HTML ...
<h1>Tom, Dick, and Harry</h1>
... arbitrary HTML ...
<h1>Fred and Barney</h1>
In other words, the my_summary() at the start of the page includes information provided later on in the page. It should be smart enough to take into account expressions which occur in include
and import
statements, as well.
What's the best way to do this?
Upvotes: 3
Views: 1553
Reputation: 100776
Disclaimer: I do not know Jinja.
My guess is that you cannot (easily) accomplish this.
I would suggest the following alternative:
Template:
{{ my_summary(list1 + list2) }} ... arbitrary HTML ... {{ my_values(list1) }} ... arbitrary HTML ... {{ my_values(list2) }}
Controller:
def a_controller_method(request): return render_template('templatefilename', { 'list1': ['Dick', 'Tom', 'Harry'], 'list2': ['Fred', 'Barney']})
{% set list1 = ['Dick', ...] %} {% set list2 = ['Fred', ...] %} {{ my_summary(list1 + list2) }} ... arbitrary HTML ... {{ my_values(list1) }} ... arbitrary HTML ... {{ my_values(list2) }}
Upvotes: 4