用 Jinja 语法迭代和打印二维数组的元素
Iterate and print elements for 2d arrays in Jinja syntax
我想迭代一个二维数组来打印每个单元格中的每个元素,我如何在 jinja 中写这个?
到目前为止,我的代码仅在单个单元格中打印每一行(包含三个元素):
Data: [[90,50,30],
[40,20,70],
[60,80,10]]
<div class="container">
<table class="table">
<thead>
<th>No</th>
<th>Value of Row 1</th>
<th>Value of Row 2</th>
<th>Value of Row 3</th>
</thead>
<tbody>
{% for i in array %}
<tr>
<td>{{ loop.index }}</td>
<td>{{ i }}</td>
<td>{{ i }}</td>
<td>{{ i }}</td>
</tr>
{% endfor %}
</div>
预期输出:
No
Value of Row 1
Value of Row 2
Value of Row 3
1
90
50
30
2
40
20
70
3
60
80
10
将您的模板更改为:
<div class="container">
<table class="table">
<thead>
<th>No</th>
<th>Value of Row 1</th>
<th>Value of Row 2</th>
<th>Value of Row 3</th>
</thead>
<tbody>
{% for row in data %}
<tr>
<td>{{ loop.index }}</td>
{% for cell in row %}
<td>{{ cell }}</td>
{% endfor %}
</tr>
{% endfor %}
</div>
我想迭代一个二维数组来打印每个单元格中的每个元素,我如何在 jinja 中写这个?
到目前为止,我的代码仅在单个单元格中打印每一行(包含三个元素):
Data: [[90,50,30],
[40,20,70],
[60,80,10]]
<div class="container">
<table class="table">
<thead>
<th>No</th>
<th>Value of Row 1</th>
<th>Value of Row 2</th>
<th>Value of Row 3</th>
</thead>
<tbody>
{% for i in array %}
<tr>
<td>{{ loop.index }}</td>
<td>{{ i }}</td>
<td>{{ i }}</td>
<td>{{ i }}</td>
</tr>
{% endfor %}
</div>
预期输出:
No | Value of Row 1 | Value of Row 2 | Value of Row 3 |
---|---|---|---|
1 | 90 | 50 | 30 |
2 | 40 | 20 | 70 |
3 | 60 | 80 | 10 |
将您的模板更改为:
<div class="container">
<table class="table">
<thead>
<th>No</th>
<th>Value of Row 1</th>
<th>Value of Row 2</th>
<th>Value of Row 3</th>
</thead>
<tbody>
{% for row in data %}
<tr>
<td>{{ loop.index }}</td>
{% for cell in row %}
<td>{{ cell }}</td>
{% endfor %}
</tr>
{% endfor %}
</div>