想要从json中获得一个值,并使用handlers.js为其添加一个徽章

Want to get a single value from json and add a badge to it using handlebars.js

本文关键字:一个 handlers js 徽章 添加 json      更新时间:2023-09-26

json数据示例:

data: [
    {name: "brian", product: "computers"},
    {name: "tom", product: "computers"},
    {name: "john", product: "computers"},
    {name: "sara", product: "machines"}
]

我想在用户旁边显示一个图像(徽章),将"机器"作为产品的价值。在车把上我该怎么做?

我解决了自己的问题,但如果有人有更好的方法,请添加。

把手助手:

  Handlebars.registerHelper('getBadges',function(product){
  var htmlVal = '';
  if(product === "machines") {
    htmlVal = '<span class="machines"><img src="../html/badge_icons/machines-small.png" alt="machines" style="width:30px; height:15px; border:none"></span>';
  } 
  return htmlVal;
});

html:

{{{getBadges product}}}