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
Templates 如何输出动态嵌套的项目符号列表?_Templates_Flask_Jinja2_Bulletedlist - Fatal编程技术网

Templates 如何输出动态嵌套的项目符号列表?

Templates 如何输出动态嵌套的项目符号列表?,templates,flask,jinja2,bulletedlist,Templates,Flask,Jinja2,Bulletedlist,我正在输出一个发生在北美的事件列表,我想按国家,然后按地区进行细分。鉴于以下数据: CA Event 1, Edmonton, AB, Canada CA Event 2, Edmonton, AB, Canada CA Event 3, Burnaby, BC, Canada BE Event 1, Brussels, NULL, Belgium US Event 1, Juneau, AK, USA US Event 2, Birmingham, AL, USA US Event 3, Si

我正在输出一个发生在北美的事件列表,我想按国家,然后按地区进行细分。鉴于以下数据:

CA Event 1, Edmonton, AB, Canada
CA Event 2, Edmonton, AB, Canada
CA Event 3, Burnaby, BC, Canada
BE Event 1, Brussels, NULL, Belgium
US Event 1, Juneau, AK, USA
US Event 2, Birmingham, AL, USA
US Event 3, Silverhill, AL, USA
我想要这样的输出

Canada
    Alberta
        CA Event 1, Edmonton, AB, Canada
        CA Event 2, Edmonton, AB, Canada
    British Columbia
        CA Event 3, Burnaby, BC, Canada
Belgium
    BE Event 1, Brussels, Belgium
USA
    Alaska
        US Event 1, Juneau, AK, USA
    Alabama
        US Event 2, Birmingham, AL, USA
        US Event 3, Silverhill, AL, USA
我目前通过输出嵌套的项目符号列表来实现这一点,但我正在努力找出如何避免重复的UL标签。同样,给定上面的输入数据,所需的HTML输出应该如下所示:

<h2>Canada</h2>
<h3>Alberta</h3>
<ul>
    <li>CA Event 1, Edmonton, AB, Canada</li>
    <li>CA Event 2, Edmonton, AB, Canada</li>
</ul>
<h3>British Columbia</h3>
<ul>
    <li>CA Event 4, Burnaby, BC, Canada</li>
</ul>
<h2>Canada</h2>
<h3>Alberta</h3>
<ul>
    <li>CA Event 1, Edmonton, AB, Canada</li>
</ul>
<ul>
    <li>CA Event 2, Edmonton, AB, Canada</li>
</ul>
<h3>British Columbia</h3>
<ul>
    <li>CA Event 4, Burnaby, BC, Canada</li>
</ul>
加拿大
阿尔伯塔省
  • 加拿大首都埃德蒙顿CA活动1
  • 加拿大首都埃德蒙顿CA活动2
不列颠哥伦比亚省
  • 加拿大不列颠哥伦比亚省伯纳比CA活动4
但现在看起来是这样的:

<h2>Canada</h2>
<h3>Alberta</h3>
<ul>
    <li>CA Event 1, Edmonton, AB, Canada</li>
    <li>CA Event 2, Edmonton, AB, Canada</li>
</ul>
<h3>British Columbia</h3>
<ul>
    <li>CA Event 4, Burnaby, BC, Canada</li>
</ul>
<h2>Canada</h2>
<h3>Alberta</h3>
<ul>
    <li>CA Event 1, Edmonton, AB, Canada</li>
</ul>
<ul>
    <li>CA Event 2, Edmonton, AB, Canada</li>
</ul>
<h3>British Columbia</h3>
<ul>
    <li>CA Event 4, Burnaby, BC, Canada</li>
</ul>
加拿大
阿尔伯塔省
  • 加拿大首都埃德蒙顿CA活动1
  • 加拿大首都埃德蒙顿CA活动2
不列颠哥伦比亚省
  • 加拿大不列颠哥伦比亚省伯纳比CA活动4
这是我的模板代码,它使用Jinja模板语言作为Flask的一部分,但任何响应都不需要它。我只是不知道如何只在需要时输出UL标签

{% set ns = namespace() %}
{% set ns.country_header = '' %}
{% set ns.region_header = '' %}

{% for convention in context.conventions %}

    {% if ns.country_header != convention.country %}
        {% set ns.country_header = convention.country %}
        <h3>{{ns.country_header}}</h3>
    {% endif %}

    {% if ns.region_header != convention.region %}
        {% set ns.region_header = convention.region %}
        <h4>{{ns.region_header}}</h4>
    {% endif %}

    <ul>
        <li>
            {{convention.name}} – 
            {{convention.city}}, {{convention.region}} {% endif %}
        </li>
    </ul>
{% endfor %}
{%set ns=namespace()%}
{%set ns.country_头=''%}
{%set ns.region_头=''%}
{%用于上下文中的约定。约定%}
{%if ns.country_头!=convention.country%}
{%set ns.country_头=convention.country%}
{{ns.country_header}
{%endif%}
{%if ns.region_头!=convention.region%}
{%set ns.region_头=convention.region%}
{{ns.region_header}
{%endif%}
  • {{convention.name}} {{convention.city},{{convention.region}{%endif%}
{%endfor%}
警告:我不是专门讲这种模板语言,但我讲的是逻辑

啊,平淡记录列表的乐趣/

当区域标题更改时发出
——基本上作为
标记的一部分

在考虑
项目之前,您必须在循环的顶部发出
,检查是否有更改

作为特殊情况,第一次进入环路时必须避免发出

您可以在
endfor
之后发出

{% for convention in context.conventions %}
    {% if <<not first record>> %}
      </ul> 
    {% endif %}

    {% if ns.country_header != convention.country %}
        {% set ns.country_header = convention.country %}
        <h3>{{ns.country_header}}</h3>
    {% endif %}

    {% if ns.region_header != convention.region %}
        {% set ns.region_header = convention.region %}
        {% if convention.region != "NULL" %}
        <h4>{{ns.region_header}}</h4>
        {% endif %}
      <ul>
    {% endif %}

        <li>
            {{convention.name}} – 
            {{convention.city}}, {{convention.region}} {% endif %}
        </li>
{% endfor %}
{% if << at least one record >> %}
    </ul>
{% endif %}
{%用于上下文中的约定。约定%}
{%if%}

{%endif%}
{%if ns.country_头!=convention.country%}
{%set ns.country_头=convention.country%}
{{ns.country_header}
{%endif%}
{%if ns.region_头!=convention.region%}
{%set ns.region_头=convention.region%}
{%if convention.region!=“NULL”%}
{{ns.region_header}
{%endif%}
    {%endif%}
  • {{convention.name}} {{convention.city},{{convention.region}{%endif%}
  • {%endfor%} {%if>%}
{%endif%}
注意:我想我在
  • 块中看到了一个额外的
    {%endif%}

    注2:如果州/省与上述
    中的最后一条记录相同,则此循环将无法发出
    记录。想想“俄罗斯/格鲁吉亚”和“美国/格鲁吉亚”。为了安全起见,您可能需要清除
    发射块中的
    ns.region\u标题


    **有人可能会因为格鲁吉亚是俄罗斯的一部分而骚扰我。但这是我能很快想到的唯一一个半明半暗的例子。

    多亏了Paul程序员,这就是我想到的。这是为子孙后代编写的完整代码,但我选择了他的回答作为答案

    {% set ns = namespace() %}
    {% set ns.active_country = '' %}
    {% set ns.active_region = '' %}
    {% set ns.open_ul = False %}
    
    {% for convention in context.conventions %}
    
        {% if not loop.first and (ns.active_country != convention.country) or (ns.active_region != convention.region) %}
            </ul>
            {% set ns.open_ul = False %}
        {% endif %}
    
        {% if ns.active_country != convention.country %}
            {% set ns.active_country = convention.country %}
            {% set ns.open_ul = False %}
            <h3>{{ns.active_country}}</h3>
        {% endif %}
    
        {% if ns.active_region != convention.region %}
            {% set ns.active_region = convention.region %}
            {% if not ns.open_ul %}
                {% set ns.open_ul = False %}
            {% endif %}
            {% if ns.active_region %}
                <h4>{{ns.active_region}}</h4>
            {% endif %}
        {% endif %}
    
        {% if not ns.open_ul %}
            <ul>
            {% set ns.open_ul = True %}
        {% endif %}
    
        <li>
            <a href="{{convention.website}}" target="_blank">{{convention.name}}</a> – 
            {{convention.city}}{% if convention.country in ['United States', 'Canada'] %}, {{convention.region[0]}} {% endif %} – 
            {{convention.date_string}}
        </li>
    
    {% endfor %}
    
    {%set ns=namespace()%}
    {%set ns.active_country=''%}
    {%set ns.active_region=''%}
    {%set ns.open_ul=False%}
    {%用于上下文中的约定。约定%}
    {%if not loop.first and(ns.active_country!=convention.country)或(ns.active_region!=convention.region)%}
    
    {%set ns.open_ul=False%}
    {%endif%}
    {%if ns.active_country!=convention.country%}
    {%set ns.active_country=convention.country%}
    {%set ns.open_ul=False%}
    {{ns.活跃国家}
    {%endif%}
    {%if ns.active_region!=convention.region%}
    {%set ns.active_region=convention.region%}
    {%如果不是ns.open_ul%}
    {%set ns.open_ul=False%}
    {%endif%}
    {%if ns.active_region%}
    {{ns.active_region}}
    {%endif%}
    {%endif%}
    {%如果不是ns.open_ul%}
    
      {%set ns.open_ul=True%} {%endif%}
    • – {{convention.city}{%if convention.country in['美国','加拿大']%},{{{convention.region[0]}{%endif%} {{convention.date_string}
    • {%endfor%}
    Paul,首先谢谢你的帮助。我意识到我遗漏了一些东西,现在我再看一遍(已经很晚了)。如果有一个国家没有地区,这种逻辑会改变吗?我将更新原始列表,以包括在一个没有地区的国家举办的活动。我认为不会有太大变化。您只需在
    (仅限于此,因此您仍然会发出
    )周围包装一个测试,以便在区域为
    NULL
    时不发出它。我已编辑了答案以显示我的意思。我认为它接近正确的语法。它几乎就在那里,但感谢你的帮助(并在新的一天编写代码),我终于得到了它。在下面发布了我的完整代码,但给出了答案。谢谢你的帮助!