admin管理员组文章数量:1404567
The following is the unsuccessful code
{{#each ['Home', 'About']}}
<p>{{this}}</p>
{{/each}}
I understand that I can define the array in js and pass it to handlebars to have it render successfully. But I want to do this only in the template. Sometimes I just need a super simple loop, like in my example.
I saw some older posts that required registering a helper
but can't the built-in helper #with
/ #each
do that?
The following is the unsuccessful code
{{#each ['Home', 'About']}}
<p>{{this}}</p>
{{/each}}
I understand that I can define the array in js and pass it to handlebars to have it render successfully. But I want to do this only in the template. Sometimes I just need a super simple loop, like in my example.
I saw some older posts that required registering a helper
but can't the built-in helper #with
/ #each
do that?
- According to this thread it seems that inline arrays are not a thing in Handlebars - Unsure if you are using ember.js, if so than this answer could help – DarkBee Commented Mar 10 at 14:37
1 Answer
Reset to default 0Handlebars doesn't parse JavaScript expressions or inline literals (like []
, +
, &&
)...it will not accept JavaScript-like expressions. You must register a helper like this in your server.js:
app.engine(
"handlebars",
engine({
helpers: {
array: (...args) => args.slice(0, -1),
},
})
);
then call it from your #each loop like:
{{#each (array 'Home' 'About')}}
<p>{{this}}</p>
{{/each}}
OR pass the array from directly your route like this:
res.render('template', { myArray: ['Home', 'About']});
and call it it your #each loop like this:
{{#each myArray}}
<p>{{this}}</p>
{{/each}}
本文标签: handlebarsjsHow to define an array and loop in a templateStack Overflow
版权声明:本文标题:handlebars.js - How to define an array and loop in a template? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744842124a2627987.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论