wuxiekeji
wuxiekeji

Reputation: 1902

Length of string in Jinja/Flask

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?

Upvotes: 77

Views: 69159

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1123850

You can use the length filter:

{% if some_var|length > 1 %}

Upvotes: 149

Related Questions