Django `带有`模板标记,逻辑比较结果为值
在当前上下文中,是否有方法将比较中的逻辑值分配给变量 我试过:Django `带有`模板标记,逻辑比较结果为值,django,templates,with-statement,Django,Templates,With Statement,在当前上下文中,是否有方法将比较中的逻辑值分配给变量 我试过: {% with hidden=forloop.counter > 4 %} {% include "path/to/template.html" %} {% endwith %} 它不起作用,因为这会导致语法错误。但也许有一个语法我不知道 到目前为止,我做了如下工作: {% if forloop.counter > 4 %} {% with hidden=True %} {% include "path
{% with hidden=forloop.counter > 4 %}
{% include "path/to/template.html" %}
{% endwith %}
它不起作用,因为这会导致语法错误。但也许有一个语法我不知道
到目前为止,我做了如下工作:
{% if forloop.counter > 4 %}
{% with hidden=True %}
{% include "path/to/template.html" %}
{% endwith %}
{% else %}
{% include "path/to/template.html" %}
{% endif %}
它可以工作,但对我来说它看起来很脏。包含
模板标签
这是真的,而且更简单。但是,您是否可以直接将比较步骤放入include标记中?(更多内容)如何在包含的模板中使用“隐藏”?您可以将计数传递到包含的模板“with count=forloop.counter”我使用它作为布尔值,以便向包含的模板中的元素添加或不添加类。但是,如果无法将比较步骤放在标记参数中,我将坚持使用if/else方法。
{% if forloop.counter > 4 %}
{% include "path/to/template.html" with hidden=True %}
{% else %}
{% include "path/to/template.html" %}
{% endif %}