【发布时间】:2021-11-26 02:32:20
【问题描述】:
假设我有一个 Django 模板
<div><b>{{some.long.expression.with.stuff|filter1}}</b></div>
如果my_condition 为真,我只想申请filter1。
最好的方法是什么?这是一种带有重复的冗长方式:
{% if my_condition %}
<div><b>{{some.long.expression.with.stuff|filter1}}</b></div>
{% else %}
<div><b>{{some.long.expression.with.stuff}}</b></div>
{% endif %}
这里稍微不那么冗长,更难阅读,仍然有一些重复:
<div><b>{% if my_condition %}{{some.long.expression.with.stuff|filter1}}{% else %}{{some.long.expression.with.stuff}}{% endif %}</b></div>
建议?
【问题讨论】:
标签: django django-templates django-template-filters