Jinja2:如何在任何循环之前识别var类型

2024-09-28 05:29:10 发布

您现在位置:Python中文网/ 问答频道 /正文

对于Jinja2,在执行 for循环?在

 Context = """
   {% macro LopyLoop(PARSER) %}
      {% for items in PARSER -%}
         {{ items }}{% if not loop.last %},{% endif %}
      {%- endfor %}
   {% endmacro %}

   {% set Mainlist =  ['ABC','XYZ']  %}

   {{ LopyLoop(Mainlist) }}
   {{ LopyLoop(Mainlist[0]) }}

 )
  """
 Output = Template(Context)
 print Output.render()

上述内容在使用[“ABC”,“XYZ”]时有效,但在使用字符串Mainlist[0]时无效

我找不到解决这个问题的方法

提前谢谢!在


Tags: inloopparserjinja2foroutputifcontext

热门问题