【问题标题】:Namespace variables and loops in jinjajinja 中的命名空间变量和循环
【发布时间】:2020-10-11 00:01:25
【问题描述】:

我正在尝试在 jinja 中使用两个命名空间变量。一个人将决定是否会显示一些 html,而另一个变量用于更改第一个。但是,只有我的 ns.show 变量有效,我很困惑

<html>
<body>

<h2>Release Notes</h2>


{% set ns = namespace(show="true")%}
{% set ns2 = namespace(current="text")%}
{% for item in items %}
<!-- This needs to only show up when scrumName is new -->
<!-- check if the scrum name new. if so use this code -->
    {% set ns = namespace(show="false")%}
    <p> 1 ; current is {{ns2.current}} | scrumName is {{item.scrumName}}</p>
    {% if ns2.current != item.scrumName %}
        {% set ns = namespace(show="true")%}
    {% endif %}

    {% set ns2 = namespace(current = item.scrumName)%}
    <p> 2; current is {{ns2.current}} | scrumName is {{item.scrumName}}</p>
    <p> show is {{ns.show}} 2  </p>
    {% if ns.show == "true" %}
        <h3>{{ item.scrumName }}</h3>
        <table style="width:100%">
            <tr>
                <th>Epic Name</th>
                <th>Story Name</th>
                <th>Release Type</th>
                <th>Release Date</th>
                <th>Epic % Completion</th>
            </tr>
    {% endif %}
<!-- ends here -->
<!-- This show up everytime -->
        <tr>
            <td>{{ item.epicName }}</td>
            <td>{{ item.storyName }}</td>
            <td>{{ item.type }}</td>
            <td>{{ item.date }}</td>
            <td>{{ item.completion }}</td>
        </tr>
<!-- ends here -->
{% endfor %}

</table>
</body>
</html>

【问题讨论】:

  • 对我来说,您应该在发送到 jinja 之前用 Python 代码重新组织数据。然后在 jinja 中你不需要命名空间。

标签: python html templates jinja2


【解决方案1】:

您错误地使用了命名空间。

您只需要一个命名空间变量,您可以为其附加多个属性。

{% set ns = namespace() %}
{% set ns.show = "true" %}
{% set ns.current = "text" %}

然后您永远不会将分配更改为 ns,您只需更新/引用在两个集合 ns.prop 实例上看到的属性。

【讨论】:

    猜你喜欢
    • 2017-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-04
    相关资源
    最近更新 更多