简体   繁体   English

在 Jinja 语法中迭代和打印二维数组的元素

[英]Iterate and print elements for 2d arrays in Jinja syntax

I want to iterate a 2d array to print every element in one cell each, how can I write this in jinja?我想迭代一个二维数组来打印一个单元格中的每个元素,我怎么能在 jinja 中写这个?

My code so far only prints each row (containing three elements) in a single cell:到目前为止,我的代码仅在单个单元格中打印每一行(包含三个元素):

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>

Expected output:预期输出:

No Value of Row 1第 1 行的值 Value of Row 2第 2 行的值 Value of Row 3第 3 行的值
1 1 90 90 50 50 30 30
2 2 40 40 20 20 70 70
3 3 60 60 80 80 10 10

Change your template to:将您的模板更改为:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM