Distinguish between types in jinja2

时光怂恿深爱的人放手 提交于 2020-01-24 20:31:05

问题


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?


回答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.




回答2:


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




回答3:


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


来源:https://stackoverflow.com/questions/49887289/distinguish-between-types-in-jinja2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!