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_Django Templates - Fatal编程技术网

多个django';与';内部声明';如果';陈述

多个django';与';内部声明';如果';陈述,django,templates,django-templates,Django,Templates,Django Templates,我在if/elif语句中有一个Django multiple'with'语句。 if/elif中的代码块除了一个变量“slide1\u line2”外,其余代码块相同。 我想知道是否有办法重新编写代码以避免重复 {% if country == 'England' or country == 'Wales'%} {% with graphic='map.png' %} {% with classes='color-1' %} {% with slide1_line1=

我在if/elif语句中有一个Django multiple'with'语句。 if/elif中的代码块除了一个变量“slide1\u line2”外,其余代码块相同。 我想知道是否有办法重新编写代码以避免重复

{% if country == 'England' or country == 'Wales'%}              
{% with graphic='map.png' %}
{% with classes='color-1' %}
{% with slide1_line1='Your constituency is '|add:name %}
{% with slide1_line2='Heading1' %}
{% with slide1_line3='text' %}
{% with icon='keyboard_arrow_down' %}
{% include 'slide_map.html' %}
{% endwith %}{% endwith %}{% endwith %}{% endwith %}{% endwith %}{% endwith %}

{% elif country == 'Scotland' or country == 'Northern Ireland' %}               
{% with graphic='map.png' %}
{% with classes='color-1' %}
{% with slide1_line1='Your constituency is '|add:name %}
{% with slide1_line2='Heading2' %}
{% with slide1_line3='text' %}
{% with icon='keyboard_arrow_down' %}
{% include 'slide_map.html' %}
{% endwith %}{% endwith %}{% endwith %}{% endwith %}{% endwith %}{% endwith %}

{% endif %}

您可以将
if
语句之外的公共值分解出来:

{% with graphic='map.png' classes='color-1' slide1_line1='Your constituency is '|add:name slide1_line3='text' icon='keyboard_arrow_down' %}
  {% if country == 'England' or country == 'Wales' %}
    {% include 'slide_map.html' with slide1_line2='Heading1' %}
  {% else %}
    {% include 'slide_map.html' with slide1_line2='Heading2' %}
  {% endif %}
{% endwith %}
请注意:

  • 您可以在同一个数据库中定义多个变量;及
  • 可以使用
    {%include…with…%}
    子句传递某些额外参数

  • if
    之外定义公共
    ,使用
    s。上述解决方案在浏览器中打印以下语句。”{%if country=='England'或country=='Wales%}{%include'tactical/slides/slide\u map.html'和slide1\u line2='Heading1%}'但是,使用/endwith正确呈现模板。'{%if country=='England'或country=='Wales'}{%with slide1\u line2='Heading1'}{%include'tactical/slides/slide_map.html'}{%endwith%}它是否与版本相关?我使用的是Django 2.2.6。@GeoMeteoMe:我在本地测试了它,它运行正常(版本2.2.2)。虽然严格地说是可能的,但如果2.2.6以不同的方式处理它,我真的会感到惊讶。你确定你没有在模板中输入错别字吗?我刚刚将Django升级到2.2.7,这没有任何区别。但我发现,{%…%}括号之间的任何内容都必须在一行中。如果{%with..%}在多行之间拆分,您将得到以下错误:第25行上的块标记无效:“endwith”,应为“endblock”。您是否忘记注册或加载此标签?如果{%include..with..%}在多行之间拆分,则语句将作为字符串打印。