Python jinja2 template, how to count a list
Question:
So I can’t use python len() for a list in the templates like below.
{% if len(alist) == 0 %}
UndefinedError: 'len' is undefined
-
How can we use python in the templates?
-
Is passing a param to the template in the def get(self) method the only way to do this?
-
Anyone know some good resources on how to use jinja2 with it comes to templating? like what methods can you use and the syntactic difference between python and jinja2.
Answers:
If you do a quick search in the template documentation you will quite soon find the length
filter.
As for the rest, read the documentation.
{% if alist |length ==0 %} or {% if alist |count ==0 %}
i Solve it with that way!!
So I can’t use python len() for a list in the templates like below.
{% if len(alist) == 0 %}
UndefinedError: 'len' is undefined
-
How can we use python in the templates?
-
Is passing a param to the template in the def get(self) method the only way to do this?
-
Anyone know some good resources on how to use jinja2 with it comes to templating? like what methods can you use and the syntactic difference between python and jinja2.
If you do a quick search in the template documentation you will quite soon find the length
filter.
As for the rest, read the documentation.
{% if alist |length ==0 %} or {% if alist |count ==0 %}
i Solve it with that way!!