如何在 jinja 模板中的 for 循环上增加变量?

发布于 2024-12-06 19:17:05 字数 160 浏览 1 评论 0原文

我想做类似的事情:

变量 p 来自 test.py,它是一个列表 ['a','b','c','d']

{% for i in p %}
{{variable++}}
{{variable}}

结果输出是:
1 2 3 4

I would like to do something like:

variable p is from test.py which is a list ['a','b','c','d']

{% for i in p %}
{{variable++}}
{{variable}}

result output is:
1 2 3 4

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

2024-12-13 19:17:06

如果有人想在循环内添加一个值,那么你可以使用它,它的工作 100%

{% set ftotal= {'total': 0} %} 
{%- for pe in payment_entry -%}
    {% if ftotal.update({'total': ftotal.total + 5}) %}{% endif %} 
{%- endfor -%}

{{ftotal.total}}

输出 = 5

if anyone want to add a value inside loop then you can use this its working 100%

{% set ftotal= {'total': 0} %} 
{%- for pe in payment_entry -%}
    {% if ftotal.update({'total': ftotal.total + 5}) %}{% endif %} 
{%- endfor -%}

{{ftotal.total}}

output = 5

青丝拂面 2024-12-13 19:17:06

来寻找 Django 的方法并找到了这篇文章。也许其他人需要 django 解决方案来到这里。

{% for item in item_list %}
    {{ forloop.counter }} {# starting index 1 #}
    {{ forloop.counter0 }} {# starting index 0 #}

    {# do your stuff #}
{% endfor %}

在这里阅读更多内容:
https://docs.djangoproject.com/en/1.11/ref/templates /内置/

Came searching for Django's way of doing this and found this post. Maybe someone else need the django solution who come here.

{% for item in item_list %}
    {{ forloop.counter }} {# starting index 1 #}
    {{ forloop.counter0 }} {# starting index 0 #}

    {# do your stuff #}
{% endfor %}

Read more here:
https://docs.djangoproject.com/en/1.11/ref/templates/builtins/

南冥有猫 2024-12-13 19:17:06

我也为这种行为而挣扎。我想根据计数器更改 jinja 中的 div 类。我很惊讶 pythonic 方式不起作用。以下代码在每次迭代时重置我的计数器,因此我只有红色类。

{% if sloupec3: %}
    {% set counter = 1 %}
    {% for row in sloupec3: %}
        {% if counter == 3 %}
            {% set counter = 1 %}        
        {% endif %} 

        {% if  counter == 1: %}
           <div class="red"> some red div </div>
        {% endif %} 

        {% if counter == 2: %}
           <div class="gray"> some gray div </div>
        {% endif %} 

        {% set counter = counter + 1 %} 

    {% endfor %}

{% endif %}

我像这样使用了loop.index并且它有效:

{% if sloupec3: %}

    {% for row in sloupec3: %} 

        {% if  loop.index % 2 == 1: %}
           <div class="red"> some red div </div>
        {% endif %} 

        {% if loop.index % 2 == 0: %}
           <div class="gray"> some gray div </div>
        {% endif %}  

    {% endfor %}

{% endif %}

I was struggle with this behavior too. I wanted to change div class in jinja based on counter. I was surprised that pythonic way did not work. Following code was reseting my counter on each iteration, so I had only red class.

{% if sloupec3: %}
    {% set counter = 1 %}
    {% for row in sloupec3: %}
        {% if counter == 3 %}
            {% set counter = 1 %}        
        {% endif %} 

        {% if  counter == 1: %}
           <div class="red"> some red div </div>
        {% endif %} 

        {% if counter == 2: %}
           <div class="gray"> some gray div </div>
        {% endif %} 

        {% set counter = counter + 1 %} 

    {% endfor %}

{% endif %}

I used loop.index like this and it works:

{% if sloupec3: %}

    {% for row in sloupec3: %} 

        {% if  loop.index % 2 == 1: %}
           <div class="red"> some red div </div>
        {% endif %} 

        {% if loop.index % 2 == 0: %}
           <div class="gray"> some gray div </div>
        {% endif %}  

    {% endfor %}

{% endif %}
撩发小公举 2024-12-13 19:17:06

只是为了让更多人了解这个问题。
Jinja2 变量的行为与传统脚本语言不同,您无法在 for 循环中修改变量。因此,要绕过此行为,您可以使用字典,因为您可以更改字典的值。

**{% set margin={"margin_value":0} %}** 
{% for lang in language %}
<ul>
    <li style="margin-right: {{ margin.margin_value}}px">{{ lang }}</li>
</ul>
**{% if margin.update({"margin_value":margin.margin_value + 2}) %} 
{% endif %}** 
{% endfor %}

在上面的代码中,字典的值被修改。

Just to shed more light into this problem.
Jinja2 variables behaves differently from that of conventional scripting languages, you can't modify the variable in a for loop.Hence to bypass this behaviour you can use a dictionary, since you can change the value of the dictionary.

**{% set margin={"margin_value":0} %}** 
{% for lang in language %}
<ul>
    <li style="margin-right: {{ margin.margin_value}}px">{{ lang }}</li>
</ul>
**{% if margin.update({"margin_value":margin.margin_value + 2}) %} 
{% endif %}** 
{% endfor %}

In the above code the value of the dictionary is being modified.

飘然心甜 2024-12-13 19:17:05

您可以使用loop.index

{% for i in p %}
  {{ loop.index }}
{% endfor %}

查看模板设计器文档

在较新的版本中,由于范围规则,以下内容将不起作用:

{% set count = 1 %}
{% for i in p %}
  {{ count }}
  {% set count = count + 1 %}
{% endfor %}

You could use loop.index:

{% for i in p %}
  {{ loop.index }}
{% endfor %}

Check the template designer documentation.

In more recent versions, due to scoping rules, the following would not work:

{% set count = 1 %}
{% for i in p %}
  {{ count }}
  {% set count = count + 1 %}
{% endfor %}
瞄了个咪的 2024-12-13 19:17:05

2.10之后,要解决范围问题,可以这样做:

{% set count = namespace(value=0) %}
{% for i in p %}
  {{ count.value }}
  {% set count.value = count.value + 1 %}
{% endfor %}

After 2.10, to solve the scope problem, you can do something like this:

{% set count = namespace(value=0) %}
{% for i in p %}
  {{ count.value }}
  {% set count.value = count.value + 1 %}
{% endfor %}
伏妖词 2024-12-13 19:17:05

正如 Jeroen 所说,存在范围问题:如果您在循环外部设置“count”,则无法在循环内部修改它。

您可以通过使用对象而不是标量来表示“计数”来克服此行为:

{% set count = [1] %}

您现在可以在 for 循环甚至 %include% 内操作计数。以下是我增加计数的方法(是的,这很笨拙,但是哦):

{% if count.append(count.pop() + 1) %}{% endif %} {# increment count by 1 #}

或者...

{% set count = [] %}
{% for something-that-loops %}
   {% set __ = count.append(1) %} 
   <div> Lorem ipsum meepzip dolor...
   {{ count|length }}
   </div>
{% endfor %}

(来自@eyettea和@PYB的评论)

As Jeroen says there are scoping issues: if you set 'count' outside the loop, you can't modify it inside the loop.

You can defeat this behavior by using an object rather than a scalar for 'count':

{% set count = [1] %}

You can now manipulate count inside a forloop or even an %include%. Here's how I increment count (yes, it's kludgy but oh well):

{% if count.append(count.pop() + 1) %}{% endif %} {# increment count by 1 #}

Or...

{% set count = [] %}
{% for something-that-loops %}
   {% set __ = count.append(1) %} 
   <div> Lorem ipsum meepzip dolor...
   {{ count|length }}
   </div>
{% endfor %}

(From comments by @eyettea and @PYB)

征棹 2024-12-13 19:17:05

这是我的解决方案:

将所有计数器放入字典中:

{% set counter = {
    'counter1': 0,
    'counter2': 0,
    'etc': 0,
    } %}

定义一个宏以轻松增加它们:

{% macro increment(dct, key, inc=1)%}
    {% if dct.update({key: dct[key] + inc}) %} {% endif %}
{% endmacro %}

现在,只要您愿意增加“counter1”计数器,只需执行以下操作:

{{ increment(counter, 'counter1') }}

Here's my solution:

Put all the counters in a dictionary:

{% set counter = {
    'counter1': 0,
    'counter2': 0,
    'etc': 0,
    } %}

Define a macro to increment them easily:

{% macro increment(dct, key, inc=1)%}
    {% if dct.update({key: dct[key] + inc}) %} {% endif %}
{% endmacro %}

Now, whenever you want to increment the 'counter1' counter, just do:

{{ increment(counter, 'counter1') }}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文