¿Cómo se indexa en una plantilla jinja?
Frecuentes
Visto 78,505 veces
33
I'm passing 3 lists to my jinja template through my python file.
list1 = [1,2,3,4]
list2 = ['a','b','c','d']
list3 = [5,6,7,8]
All these values correspond with eachother, so 1 matches with 'a' and 5, 2 with 'b' and 6, etc.
In my template I'm printing them out on the same line. How do I do numerical indexing to print them out? As so
1 a 5
2 b 6
3 c 7
The only thing I know is directly accessing the object through the loop like
{%for item in list%}
{{item}}
4 Respuestas
45
If you really want the index, you could just loop on one of the variables and then uses Jinja's loop.index0
feature (returns the current index of the loop starting at 0 (loop.index
does the same thing, starting at 1)
Por ejemplo:
{% for item in list1 %}
{{ item }}
{{ list2[loop.index0] }}
{{ list3[loop.index0] }}
{% endfor %}
This assumes your lists are all asserted to be the same length before setting the template, or you'll encounter problems.
Respondido el 30 de enero de 14 a las 14:01
the difference between loop.index0 and loop.index saved me - Barbz_YHOOL
10
Dos caminos:
In your code that calls Jinja simply
zip
your lists:data = zip(list1, list2, list3) # data is now a list of tuples # [(1, 'a', 5), (2, 'b', 6), etc.]
Then, in your template you can simply loop over the nested rows:
{# your_template.jinja #} <table> {% for row in data %} <tr> {% for cell in row %} <td>{{ cell }}</td> {% endfor %} </tr> {% endfor %} </table>
As an alternate, if you only want to use Jinja you can use the special
loop
variable:<table> {% for cell in list1 %} <tr> <td>{{ list1[loop.index0] }}</td> <td>{{ list2[loop.index0] }}</td> <td>{{ list3[loop.index0] }}</td> </tr> {% endfor %} </table>
respondido 27 nov., 13:05
4
Similar to @Sean Vieira answer, you can zip the data in your code, then index it in the template. For example:
data = zip(list1, list2, list3)
<table>
<tr>
<td>list 1 value</td>
<td>list 2 value</td>
<td>list 3 value</td>
<tr>
{% for row in data %}
<tr>
<td>{{ row[0] }}</td>
<td>{{ row[1] }}</td>
<td>{{ row[2] }}</td>
</tr>
{% endfor %}
</table>
Respondido 07 Oct 15, 01:10
2
Not directly but indirectly you can do list indexing
First of all convert the your list in string then pass in your jinja template
Second split that string into set(similar to list in list) in the jinja template by splitting it.
Then you can do simple indexing as you do in python lists.
{% set list1 = variable1.split(',') %}
The grass is {{ list1[0] }} and the boat is {{ list1[1] }}
or
{% set list1 = variable1.split(',') %}
{% for item in list1 %}
<p>{{ item }}<p/>
{% endfor %}
or
{% set item1, item2 = variable1.split(',') %}
The grass is {{ item1 }} and the boat is {{ item2 }}
respondido 08 mar '21, 09:03
why is the split character ";" when the tuple is delimited by ","? - Fred Zimmermann
Oh !, I forgot it to change. Thanks You For Noticing This - Dhruv Arne
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas python jinja2 or haz tu propia pregunta.
utilizan el
{ % for i, item enumerate(l) % }
- Grijesh ChauhanIn the view, you can do
zip(list1, list2, list3)
and then loop through them - karthikr@GrijeshChauhan I don't think you can do enumerate in jina2. i am getting errors that it is not defined - bernie2436
@bernie2436 the code I commented is missing
in
in for syntax, corected would be{ % for i, item in enumerate(l) % }
. But I don't know Jina. - Grijesh Chauhan