我已经想尽了各种办法,但仍然没有解决方案。也许有人有建议?
我在一个使用Nunjucks作为模板引擎的ExpressJS应用程序中有以下代码块。
{%- for course in courses -%}
{%- for rcourse in report.courses -%}
{%- if rcourse.id == course.id -%}
<li {{ 'class="column-pre"' if loop.last else ''}}><span>Cool!</span></li>
{%- else -%}
<li></li>
{%- endif -%}
{%- endfor -%}
{%- endfor -%}
我的问题:当条件为truth-y时,我需要在内部的 report.courses
循环中使用 break
。 基本上,一旦我打印出非空的<li>
行,我需要跳到 courses
循环的下一个迭代。
我知道Nunjucks没有像Jinja2那样适用于循环的 break
,而且Nunjucks的变量是作用域的(因此我不能设置类似哨兵的变量,在 if / else
语句中修改它),也不能将元素附加到数组上,以便我可以使用 array.length
来确定是否应打印<li></li>
行。
也许有人有巧妙的解决方案?