我的 mustache 模板未渲染,我不明白为什么不渲染。
var output = Mustache.render('{{#languages}} {{@index}} {{locale}} {{/languages}}', '{"languages" : [ { "locale" : "english", "code" : "en" }, { "locale" : "deutsch", "code" : "de" }, { "locale" : "español", "code" : "es" }, { "locale" : "français", "code" : "fr" } ]}');
此输出为空。我还希望索引与每个项目一起呈现。
最佳答案
第一个问题是您应该传递一个对象作为数据,而不是字符串。删除 languages
对象周围的引号。
第二个问题是 Mustache 不支持 {{@index}}
特殊引用。许多类似 Mustache 的模板系统,例如 Dust.js和 Handlebars.js确实本质上支持这一点,但在 Mustache 中,您必须编写一个助手并将其包含在数据中。为此,请参阅有关该主题的许多其他 StackOverflow 答案之一,例如 this one .
关于javascript - Mustache.js 每个循环都不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29261761/