Jinja / Flask中的弦长

时间:2021-01-29 06:02:00

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

不幸的是,Jinja不支持执行任意Python代码,例如

{% 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?

虽然这有效,但我担心未来某些字符串的实现可能会破坏这段代码。有一个更好的方法吗?

1 个解决方案

#1


61  

You can use the length filter:

您可以使用长度过滤器:

{% if some_var|length > 1 %}

#1


61  

You can use the length filter:

您可以使用长度过滤器:

{% if some_var|length > 1 %}