Length of string in Jinja/Flask

PythonFlaskJinja2String Length

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

You can use the length filter:

{% if some_var|length > 1 %}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionwuxiekejiView Question on Stackoverflow
Solution 1 - PythonMartijn PietersView Answer on Stackoverflow