How to get HTML from JSON data, Using MUSTACHE?
I am using JSON and MUSTACHE, for templates in my new site. But I don't know how can i get the HTML out of json data. I am using PHP in backend, which is working as API provider. I am very new to this concept. Evey help and suggestions are Welcome.
Thanks.
Code I am using::
<script>
// this is for base
this.get(/\#\/(.*)/, function (){
var sen开发者_开发问答d_url = '<?php echo $url?>sammy/' + this.params['splat'];
var context = this;
$.getJSON(send_url, function(data) {
var template = data.template;
context.renderEach('<?php echo $url?>mustache_templates/' + template + '', data.data).swap();
});
});
</script>
JSON data is like::
{"menu": {
"id": "file",
"string": "<a href=\"http:\/\/stackoverflow.com\/questions\/5335873\/how-to-get-html-from-json-data-using-mustache#xyz\">string</a>",
}}
MUSTACHE template:
{{#string}}
<div>
{{string}}
</div>
{{/string}}
Current Output:
<a href="https://stackoverflow.com/questions/5335873/how-to-get-html-from-json-data-using-mustache#xyz">string</a>
Output Needed:
string
Thanks you
Current Output:
<a href="http://stackoverflow.com/questions/5335873/how-to-get-html-from-json-data-using-mustache#xyz">string</a>
That is not the output. What you actually get is something like
<a href="http://stackoverflow.com/questions/5335873/how-to-get-html-from-json-data-using-mustache#xyz">string</a>
Which of course looks like what you've posted in a browser. Mustache HTML escapes all your variables by default, so they don't mess up your HTML.
In this case you don't want that, so you should use {{{string}}}
in the template. Be sure to only do that with trusted variables, never use it to output any user input.
Take a look at json_decode in PHP, that'll get you an array of your data which you (i presume) can feed to your templating engine:
$json = <<< EOJ
{"menu": {
"id": "file",
"string": "<a href=\"http:\/\/stackoverflow.com\/questions\/5335873\/how-to-get-html-from-json-data-using-mustache#xyz\">string</a>",
}}
EOJ;
$json_parsed = json_decode($json_raw, TRUE); // return an array, not an object
DoYourMustacheThingWith($json_parsed);
精彩评论