有条件的Jinja2速记 [英] Jinja2 shorthand conditional
本文介绍了有条件的Jinja2速记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我有这个:
{% if files %}
Update
{% else %}
Continue
{% endif %}
例如,在PHP中,我可以写一个简写的条件语句,例如:
In PHP, say, I can write a shorthand conditional, like:
<?php echo $foo ? 'yes' : 'no'; ?>
然后有一种方法可以将其转换为在jinja2模板中工作:
Is there then a way I can translate this to work in a jinja2 template:
'yes' if foo else 'no'
推荐答案
是的,如果-表达式:
Yes, it's possible to use inline if-expressions:
{{ 'Update' if files else 'Continue' }}
这篇关于有条件的Jinja2速记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文