Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading