在jinja2中的loop.first上初始化变量在版本2.8中有效,但在版本2.9.5中没有

时间:2017-12-04 20:29:47

标签: jinja2 pyramid

我在jinja2版本2.8中应用了以下逻辑,代码按预期工作:

{%- for x in timeline -%}
  {%- if loop.first -%}
    {%- set N = 0 -%}
    ...do something with N...
  {%- else -%}
    {%- if prevActionType != "some string" -%}
      {%- set N = N + 1 -%}
      ...do something with N...
    {%- endif -%}
  {%- endif -%}
  {%- set prevActionType = x.actionType -%}
{%- endfor -%}

可以看出,我需要与循环迭代次数分别跟踪N

我已将jinja2升级到版本2.9.5,现在我收到以下错误:

...
{%- set N = N + 1 -%}
UndefinedError: 'N' is undefined

在我看来,如果timeline数据有1行或更多行,N将始终初始化为值0.根据jinja2文档,if语句不会引入新范围。我的整个代码块只有一个for循环,而N仅在此for循环中使用。

底层的python版本是2.7.x.升级后为什么会出现此错误?

0 个答案:

没有答案