用树枝在 table 中插入行

insert row in table with twig

我使用我的代码来填充 table : 我在县牢房和区牢房中有两个记录 这是我的代码:

<table>
    <tr>
    <th>town</th>
    <th>counties</th>
    <th>districts</th>
    <th>number doctor</th>
    </tr>
    {% for item in locality %}
        <tr>
          <td>{{ item.name }}</td>

          <td>
            {% for list in item.regions %}
                {{ list.county}}
            {% endfor %}
          </td>

           <td>
            {% for list in item.regions %}
                {{ list.code}}
            {% endfor %}
           </td>

          <td>{{ item.regions|default([])|length }}</td>
        </tr>
    {% endfor %}
</table>

这是我的 table:

+--------+---------+----------+---------------+
|   town |counties |districts | number doctor |
+--------+---------+----------+---------------+
| Adan   | Afla    | avo      |               |
|        | kent    | joly     | 2             |
+--------+---------+----------+---------------+

但是,我希望 table 显示变为:

+----------+---------+----------+---------------+
| town     |counties |districts | number doctor |
+----------+---------+----------+---------------+
| Adan     | Afla    |   avo    | 2             |
+----------+---------+----------+---------------+
| Adan     | kent    |   joly   | 2             |
+----------+---------+----------+---------------+

如何解决这个问题?

注意:请原谅我的英语,

提前致谢

您只需要将 for 循环放置在不同位置即可读出您的数据,例如

<table>
    <tr>
    <th>town</th>
    <th>counties</th>
    <th>districts</th>
    <th>number doctor</th>
    </tr>
{% for item in locality %}
    {% for list in item.regions|default([]) %}
    <tr>
        <td>{{ item.name }}</td>
        <td>{{ list.county}}</td>
        <td>{{ list.code}}</td>
        <td>{{ item.regions|length }}</td>
    </tr>
    {% endfor %}
{% endfor %}
</table>

demo