如何比较Handlebar中的对象值?

huangapple go评论55阅读模式
英文:

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}}
      &lt;p&gt;User Rating is 5!&lt;/p&gt;
    {{else math user_rating 4}}
      &lt;p&gt;User rating is 4&lt;/p&gt;	
    {{else}}
      &lt;p&gt;User rating is less than 4&lt;/p&gt;
    {{/math}}
  {{/each}}
`);

Handlebars.registerHelper(&#39;math&#39;, function(v1, v2,options) {
  if (v1 == v2) {
    return options.fn(this);
  } else {
    return options.inverse(this);
  }
});

const data = {
  paper: [
    {
      user_name: &quot;xyz&quot;,
      user_email: &quot;abc@gmail.com&quot;,
      user_rating: 4
    },
    {
      user_name: &quot;pqr&quot;,
      user_email: &quot;xyz@gmail.com&quot;,
      user_rating: 5,
    }
  ]
};

const output = template(data);

document.body.innerHTML = output;

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.7.7/handlebars.min.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月17日 16:25:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269994.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定