Manza
Manza

Reputation: 2141

validate mustache tag value from json file

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}}

Upvotes: 1

Views: 1085

Answers (2)

Yevhenii Herasymchuk
Yevhenii Herasymchuk

Reputation: 2137

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.

Upvotes: 1

Muntasim
Muntasim

Reputation: 6786

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}}

Upvotes: 2

Related Questions