简体   繁体   English

块分配内的循环?

[英]For loop inside block assignment?

Here's an elementary loop, which compiles fine when outside of an assignment block:这是一个基本循环,在赋值块之外编译得很好:

{% for item in items %}
  <p>{{item}}</p>
{% endfor %}

But when I place the loop inside of an assignment block , like so但是当我将循环放在赋值块内时,就像这样

{% set stuff %}
  {% for item in items %}
    <p>{{item}}</p>
  {% endfor %}
{% endset %}

I get AssertionError: Tried to resolve a name to a reference that was unknown to the frame ('item') .我得到AssertionError: Tried to resolve a name to a reference that was unknown to the frame ('item')

The motivation for the question is that I am using macros to avoid code duplication.这个问题的动机是我使用宏来避免代码重复。 Eg, I have a number of divs with different fields.例如,我有许多具有不同字段的 div。 One of the divs contains a message to the user.其中一个 div 包含给用户的消息。 In one (but only one) case, I would like to include a <ul> in this div, and so I would like to loop through the elements in a list, wrapping each of them in <li> tags, before passing the resulting html as an argument to the macro.在一种(但只有一种)情况下,我想在此 div 中包含一个<ul> ,因此我想循环遍历列表中的元素,将每个元素包装在<li>标签中,然后传递结果html 作为宏的参数。 Hence my question.因此我的问题。

Is it possible to use a for loop inside of an assignment block?是否可以在分配块内使用 for 循环? Or is there a better way of achieving the same thing?还是有更好的方法来实现同样的目标?

Based on AssertionError: Tried to resolve a name to a reference that was unknown to the frame this problem is only in Jinja2 versions 3.x .基于AssertionError: 试图将名称解析为框架未知的引用,此问题仅在Jinja2版本3.x中出现。 Older versions 2.x works correctly.旧版本2.x可以正常工作。

At this moment it needs to set variable before you use it in block.此时它需要在块中使用它之前设置变量。 Maybe later they fix it.也许稍后他们会修复它。

{% set item = None %}

{% set stuff %}
  {% for item in items %}
    <p>{{item}}</p>
  {% endfor %}
{% endset %}

{{ stuff }}

But set has one big drawback for me: it can't get arguments and it works only with items .但是set对我来说有一个很大的缺点:它不能得到 arguments 并且它只适用于items
And it can't works if I set {% set items = other_items %}如果我设置{% set items = other_items %}它不能工作

I would rather put code in macro to use stuff(main_items) , stuff(other_items) , etc.我宁愿将代码放在macro中以使用stuff(main_items)stuff(other_items)等。

{% macro stuff(items) %}
  {% for item in items %}
    <p>{{item}}</p>
  {% endfor %}
{% endmacro %}

{{ stuff(main_items) }}

{{ stuff(other_items) }}

Minimal working code:最小的工作代码:

from flask import Flask, render_template_string

app = Flask(__name__)

@app.route('/')
def index():
    return render_template_string('''
<h1>SET</h1>

{% set item = None %}

{% set stuff_set %}
  {% for item in items %}
    <p>{{item}}</p>
  {% endfor %}
{% endset %}

{{ stuff_set }}

<h1>MACRO</h1>

{% macro stuff_macro(items) %}
  {% for item in items %}
    <p>{{item}}</p>
  {% endfor %}
{% endmacro %}

{{ stuff_macro(main_items) }}
{{ stuff_macro(other_items) }}
''', items=['A', 'B', 'C'], main_items=[1,2,3], other_items=[4,5,6])

if __name__ == '__main__':
    #app.debug = True 
    app.run()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM