最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Handlebars math expressions when looping through array - Stack Overflow

programmeradmin5浏览0评论

I'm practicing node, and I ran to a little problem. I'm using handlebars as a templating engine, and mongoose for interaction with my database. In my .hbs template I am looping through each record from the database, and of course I passed the records to my view with

router.get('/', function(req, res, next) {
Exam.find(function(err, predmeti) {
if (err) {
  console.log(err);
}
  res.render('index',{
    predmeti: predmeti
  })
})

}); 

Now when im looping through predmeti with #each, engine throws an error when I try doing math expressions such as

{{#each predmeti}}
 <h1>{{100/(brKolokvijuma/finished)}}</h1>
{{/each}}

brKolokvijuma and finished both have a number value, but for some reason I get a error Expecting ID got OPEN_SEXPR

So I'm assuming handlebars won't let me do math expressions when I'm looping through an array. How can I solve this?

I'm practicing node, and I ran to a little problem. I'm using handlebars as a templating engine, and mongoose for interaction with my database. In my .hbs template I am looping through each record from the database, and of course I passed the records to my view with

router.get('/', function(req, res, next) {
Exam.find(function(err, predmeti) {
if (err) {
  console.log(err);
}
  res.render('index',{
    predmeti: predmeti
  })
})

}); 

Now when im looping through predmeti with #each, engine throws an error when I try doing math expressions such as

{{#each predmeti}}
 <h1>{{100/(brKolokvijuma/finished)}}</h1>
{{/each}}

brKolokvijuma and finished both have a number value, but for some reason I get a error Expecting ID got OPEN_SEXPR

So I'm assuming handlebars won't let me do math expressions when I'm looping through an array. How can I solve this?

Share Improve this question asked Dec 21, 2016 at 11:49 OgnjenOgnjen 1051 gold badge2 silver badges10 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 13

There are plugins for Handlebars to enable you to do the things they think you shouldn't do in views, but everyone still wants to.

For example the Assemble.io maths helpers and nested expressions would enable you to do something like:

{{#each predmeti}}
   <h1>{{divide 100 (divide brKolokvijuma finished)}}</h1>
{{/each}}

A second method, if your maths expressions are not so varied and reused around your views (mon calculations e.g. tax or rounding), you could write your own simpler/lighter plugin:

Handlebars.registerHelper("divideMyThings", function(thing1, thing2, thing3) {
  return thing1 / thing2 / thing3;
});

Called in your template:

{{divideMyThings 100 brKolokvijuma finished}}

Thirdly you can often prepute data-tables until you only have one variable, and then use the builtin lookup feature. This requires no additional plugins. This is probably not helpful to this specific question, but I thought I'd mention it as a tool.

发布评论

评论列表(0)

  1. 暂无评论