Check if an array is not empty in Jinja2

后端 未结 7 1127
萌比男神i
萌比男神i 2021-02-06 20:48

I need to check if the variable texts is defined or not in index.html.

If the variable is defined and not empty then I should render the loop.

7条回答
  •  感情败类
    2021-02-06 21:22

    I think your best bet is a combination of defined() check along with looking at the length of the array via length() function:

    {% if texts is defined and texts|length > 0 %}
        ...
    {% endif %}
    

提交回复
热议问题