Python jinja2 shorthand conditional

﹥>﹥吖頭↗ 提交于 2019-12-18 10:31:36

问题


Say I have this:

{% if files %}
    Update
{% else %}
    Continue
{% endif %}

In PHP, say, I can write a shorthand conditional, like:

<?php echo $foo ? 'yes' : 'no'; ?>

Is there then a way I can translate this to work in a jinja2 template:

'yes' if foo else 'no'

回答1:


Yes, it's possible to use inline if-expressions:

{{ 'Update' if files else 'Continue' }}



回答2:


Alternative way (but it's not python style. It's JS style)

{{ files and 'Update' or 'Continue' }}


来源:https://stackoverflow.com/questions/14214942/python-jinja2-shorthand-conditional

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!