validate mustache tag value from json file

╄→гoц情女王★ 提交于 2019-12-11 06:40:00

问题


Hi I havent been able to find how to validate a tag before displaying it, for instance: if I have a JSON file lets says this values:

{ "weeks":
  [
    {
      "monday800": "no",
      "monday830": "available",
      "tuesday800": "available",
      "tuesday830": "no",
      "name": "TEST"
    }
  ]
}

I bring the value like this:

$(function(){
    $.get("data.json",function(data){
        var template = $("#tpl").html();
        var html = Mustache.to_html(template,data);
        console.log ();
        $("#information").html(html);
    });
});

Now what I want to do is: if {{monday830}} == "available" then display {{name}}


回答1:


Its not possible in mustache as its logic less. Use handlebarsjs instead.

If you still want to achieve this in mustache then:

You can not use any if statement but you can check if anything exist or its value is true.

if this case you have to update json like:

{ "weeks": [    {
      "monday800": "no",
      "monday830": "available",
      "monday830_available": true, // you have to add another property here
      "tuesday800": "available",
      "tuesday830": "no",
      "name": "TEST"
     }
   ]
 }

Then you can check

{{#monday830_available}}
     ..............
{{/monday830_available}}



回答2:


I have struggled with the same problem as the author described in a comment on the question and according to the mustache library, you cannot do it. There is an issue about it. But this problem is widespread and there is a fork to help you. Here is the link. It works like should.



来源:https://stackoverflow.com/questions/17269415/validate-mustache-tag-value-from-json-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!