How to create a tag in Jinja that contains values ​​from a later template?

I use Jinja2, and I'm trying to create a couple of tags that work together, so 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') }}

As a result, I get 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, my_summary () at the top of the page contains information presented later on the page. He must be smart enough to take into account the expressions that occur in the includeand operators import.

What is the best way to do this?

+3
source share
1 answer

Disclaimer: I do not know Jinja.

I assume that you cannot (easily) accomplish this.

I would suggest the following alternative:

  • Tom, Dick .. .
  • .
  • , "" . -, "", , .
  • :

:

{{ my_summary(list1 + list2) }}
... arbitrary HTML ...
{{ my_values(list1) }}
... arbitrary HTML ...
{{ my_values(list2) }}

:

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) }}
+4

Source: https://habr.com/ru/post/1703019/


All Articles