insert row in table with twig

597 views Asked by At

I use my code to populate table : i have two record in counties cell and districts cell here is my code:

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

here is my table:

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

however, I would like the table display to become :

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

How resolve this issue ?

NB: excuse my english,

thank you in advance

1

There are 1 answers

0
DarkBee On BEST ANSWER

You just need to place your for-loops different to read out your data, e.g.

<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