1

I have a HTML/jinja2 template that I am using to display data in the form of a table.

The data for the table may come in different ways, and I want the template to be able to handle both types using if statements. the type(x) function in python does not work in this language.

  1. an array of dictionaries (list of dictionaries)

  2. an array of arrays (list of lists)

a part of the template:

{% block page_content %}
<input type="text" id="search" placeholder="Type to search">
<table id="table" class="table">
<tr style="white-space:nowrap;">
    {% for h in headers %}
    {% if h is string %}
    <th>{{h}}</th>
    {% else %}
    <th>{{h[1]}}</th>
    {% endif %}
    {% endfor %}
</tr>
{%if data is "TYPE CHECK HERE"}
{% for row in data %}
{% if row != {} %} #ALTERNATIVELY, COULD DO A TYPE CHECK HERE
<tr style="white-space:nowrap;{% if row['bad'] %}background-color:rgba(255, 0, 0, 0.09);{% endif %}">
    {% for h in headers %}
    <td style="white-space:nowrap;">{{ row[h[0]] }}</td>
    {% endfor %}
</tr>
{% endif %}
{% endfor %}

{% endblock %}

TL:DR What are the distinguishable types in jinja2? How do I check a variable's type?

Josh Sharkey
  • 1,008
  • 9
  • 34
  • [This](http://jinja.pocoo.org/docs/2.10/templates/#builtin-tests) might be helpful! – Matt Apr 17 '18 at 21:00

3 Answers3

1

It's better practice to keep as much as your logic code on the python side, before rendering jinja.

So transfer your data on the python side to a list of headers, and a nested list as your data:

render_template('test.html', 
                headers=['name', 'age',], 
                rows=[{'color': 'red', 'data': ['john', 84]},
                      {'color': 'green', 'data':['jacob', 45]}]

html:

 <table>
  <thead>
    <tr>
    {% for item in header %}
      <th> {{ item }} </th>
    {% endfor %}
    </tr>
  </thead>

  <tbody>
  {% for row in rows %}
    <tr style="colour: {{ row['color'] }}">
    {% for cell in row['data'] %}
      <td>{{cell}}</td>
    {% endfor %}
    </tr>
  {% endfor %}
  </tbody>
</table>

I've also tried approaches similar to yours while learning, but in the end I found that this works best because it gives you the most flexibility over your data, and manipulating data on the python side is often quite straightforward. So not really an answer, just general advise.

Joost
  • 3,609
  • 2
  • 12
  • 29
1

You could look this link, there describe some valid types to compare. For instance, you could check is a dict, you try:

{% if type({'a':1,'b':2}) is mapping %}
     print "Oh Yes!!"
{% else %}
    print "Oh No!!!"
{% endif %}

You can nested whatever you need, but the right way here is migrate complex logic to controller.

PD: This example was taken from here. Thank you to @sean-vieira

0
  1. my_var is list
my_var = []
my_var.insert(0, 'list')
  1. my_var is str
my_var = '111'
  1. to distinguish in jinja2
{% if my_var[0] == 'list' %}
list: 
{% for i in my_var[1:] %}
{{ i }}
{% endfor %}

{% else%}
str: {{ my_var }}
{% endif %}
admin
  • 169
  • 1
  • 6