Jinja / Flask中的字符串长度 [英] Length of string in Jinja/Flask

查看:660
本文介绍了Jinja / Flask中的字符串长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我当前的解决方法是使用弃用的丑陋的双-underscore方法:

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

虽然这个方法可行,恐怕以后一些字符串的实现可能会破坏这个代码。有没有更好的方法来做到这一点? .org / docs / templates /#length> length 过滤器:

  {%if some_var | length> 1%} 


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?

解决方案

You can use the length filter:

{% if some_var|length > 1 %}

这篇关于Jinja / Flask中的字符串长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆