Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django `带有`模板标记,逻辑比较结果为值_Django_Templates_With Statement - Fatal编程技术网

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 %}