简体   繁体   English

Nunjucks在模板循环中引发错误

[英]Nunjucks throws error in template loop

I have the following code: 我有以下代码:

var items = [{'name': 'item1'}, {'name': 'item2'}]
var items = nunjucks.render('items.html', items);

And template: 和模板:

{% for item in items %}
    {{ item.name }}
{% endfor %}

I get the following error: 我收到以下错误:

TypeError: s.fromIterator is not a function
at Object.e.prettifyError (nunjucks-slim.min.js:2)
at nunjucks-slim.min.js:2

When I try render a single item, ie not in an array and no loop in the template, it renders fine and there is no issue. 当我尝试渲染单个项目时,即不在数组中且模板中没有循环时,它可以正常渲染并且没有问题。 The error isn't really much to go on and nothing turns up in a google - any idea what might be wrong or how to troubleshoot further? 该错误的发生并不多,并且在Google中未发现任何错误-任何想法可能出问题或如何进一步排除故障?

Try change var items = nunju... to nunju... . 尝试将var items = nunju...更改为nunju...

// example 
var nunjucks  = require('nunjucks');
var env = nunjucks.configure();
var items = [{'name': 'item1'}, {'name': 'item2'}];

var res = nunjucks.renderString(`
    {% for item in items %}
    {{item.name}}
    {% endfor %}`, 
    {items}
);

console.log(res);

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

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