英文:
How to Compare Object Values In Handlebar?
问题
这是我的对象数组:
paper: [
{
"commentDate": "17-05-2023",
"id": 1,
"user_name": "xyz",
"user_email": "abc@gmail.com",
"user_rating": 4,
"user_review": "woow",
"commentTime": "11:03:51",
"paperId": 2,
"userId": 3
},
{
"commentDate": "17-05-2023",
"id": 3,
"user_name": "pqr",
"user_email": "xyz@gmail.com",
"user_rating": 5,
"user_review": "very nice!",
"commentTime": "11:17:57",
"paperId": 2,
"userId": 4
}
]
在我的Handlebars页面中,我想打印出数据,例如:
{{#each paper}}
{{user_name}}
{{user_email}}
{{/each}}
现在我想在每个块下执行一些if-else逻辑,如下所示:
{{#each paper}}
{{user_name}}
{{user_email}}
{{#if (eq user_rating 4)}}
// 一些逻辑
{{else if (eq user_rating 3)}}
// 一些逻辑
{{else}}
// 一些逻辑
{{/if}}
{{/each}}
这样我可以实现。请帮助我。
这是我的helper:
'math': function(v1, v2, options) {
if (v1 == v2) {
return options.fn(this);
} else {
return options.inverse(this);
}
}
我尝试了以下内容:
{{#each paper}}
{{user_name}}
{{user_email}}
{{#math user_rating 5}}
// 一些逻辑
{{else}}{{#math user_rating 4}}
// 一些逻辑
{{else}}
// 一些逻辑
{{/math}}
{{/each}}
但它显示错误信息:"math does not match with each"。
请帮助我。
英文:
This is my array of objects:
paper: [
{
"commentDate": "17-05-2023",
"id": 1,
"user_name": "xyz",
"user_email": "abc@gmail.com",
"user_rating": 4,
"user_review": "woow",
"commentTime": "11:03:51",
"paperId": 2,
"userId": 3
},
{
"commentDate": "17-05-2023",
"id": 3,
"user_name": "pqr",
"user_email": "xyz@gmail.com",
"user_rating": 5,
"user_review": "very nice!",
"commentTime": "11:17:57",
"paperId": 2,
"userId": 4
}
]
In my handlebars page, I want to print data such as:
{{#each paper}}
{{user_name}}
{{user_email}
{{/each}}
Now I want to perform some if else logic under each block like below:
{{#each paper}}
{{user_name}}
{{user_email}
if (user_rating == 4 ){
} else if(user_rating == 3) {
}else{
}
{{/each}}
How can I do this? Please help me out.
This my helper:
'math': function(v1, v2,options){
if(v1 == v2){
return options.fn(this);
}
else{
return options.inverse(this);
}
}
And I tried the following:
{{#each paper}}
{{user_name}}
{{user_email}
{{#math user_rating 5}}
//some logic
{{else}}{{#math user_rating 4}}
//some logic
{{else}}
//some logic
{{/math}}
{{/each}}
And it says, "math does not match with each".
Please Help me.
答案1
得分: 0
您没有使用相应的 {{/math}}
关闭您的第二个 #math
块。然而,即使您这样做了,您可能也不会得到您期望的输出,因为在第二个 else
分支之前,它会被 else
分支处理。您想要做的是与 math
助手一起使用 else
,就像这样:{{else math user_rating 4}}
。
英文:
You are not closing your second #math
block with a corresponding {{/math}}
. However, even if you were, you would probably not get the output you expect because nothing would ever get to the second else
branch because it would get handled by the else
branch before it. What you want to do is to use else
with the math
helper, as in {{else math user_rating 4}}
.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const template = Handlebars.compile(`
{{#each paper}}
{{user_name}}
{{user_email}}
{{#math user_rating 5}}
<p>User Rating is 5!</p>
{{else math user_rating 4}}
<p>User rating is 4</p>
{{else}}
<p>User rating is less than 4</p>
{{/math}}
{{/each}}
`);
Handlebars.registerHelper('math', function(v1, v2,options) {
if (v1 == v2) {
return options.fn(this);
} else {
return options.inverse(this);
}
});
const data = {
paper: [
{
user_name: "xyz",
user_email: "abc@gmail.com",
user_rating: 4
},
{
user_name: "pqr",
user_email: "xyz@gmail.com",
user_rating: 5,
}
]
};
const output = template(data);
document.body.innerHTML = output;
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.7.7/handlebars.min.js"></script>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论