Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
374 views
in Technique[技术] by (71.8m points)

python - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use the length filter:

{% if some_var|length > 1 %}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...