Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python jinja2 template, how to count a list [duplicate]

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.

like image 786
tipsywacky Avatar asked Feb 05 '13 08:02

tipsywacky


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

like image 131
Some programmer dude Avatar answered Sep 20 '22 00:09

Some programmer dude


{% if alist |length ==0 %}  or  {% if alist |count ==0 %} 

i Solve it with that way!!

like image 32
buoge Avatar answered Sep 21 '22 00:09

buoge