70

Jinja unfortunately does not support executing arbitrary Python code, such as

{% if len(some_var)>1 %} ... {% endif %}

My current workaround is to use the deprecated, ugly, double-underscore method:

{% if some_var.__len__()>1 %} ... {% endif %}

Although this works, I'm afraid that some future implementation of strings might break this code. Is there a better way to do this?

dreftymac
  • 31,404
  • 26
  • 119
  • 182
wuxiekeji
  • 1,772
  • 2
  • 15
  • 22
  • 3
    Note that despite not being the right solution here (use `|length` as indicated to you in the answers), `__len__` is not deprecated. – Thomas Orozco Jun 11 '14 at 13:01

1 Answers1

139

You can use the length filter:

{% if some_var|length > 1 %}
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343