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
  1. How can we use python in the templates?

  2. Is passing a param to the template in the def get(self) method the only way to do this?

  3. 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.

Asked By: tipsywacky

||

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.count() == 0 %}

That should solve your problem.

You can check out this link.

Answered By: Sunkanmi Akintoye
{% if alist |length ==0 %}  or  {% if alist |count ==0 %}

i Solve it with that way!!

Answered By: buoge
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.