我正在使用JSON和MUSTACHE,用于新网站中的模板.但我不知道如何从json数据中获取HTML.我在后端使用PHP,它作为API提供者工作.我对这个概念很陌生.Evey的帮助和建议是欢迎的.
谢谢.
我正在使用的代码::
<script>
// this is for base
this.get(/\#\/(.*)/, function (){
var send_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>
Run Code Online (Sandbox Code Playgroud)
JSON数据就像::
{"menu": {
"id": "file",
"string": "<a href=\"http:\/\/stackoverflow.com\/questions\/5335873\/how-to-get-html-from-json-data-using-mustache#xyz\">string</a>",
}}
Run Code Online (Sandbox Code Playgroud)
MUSTACHE模板:
{{#string}}
<div>
{{string}}
</div>
{{/string}}
Run Code Online (Sandbox Code Playgroud)
电流输出:
<a href="https://stackoverflow.com/questions/5335873/how-to-get-html-from-json-data-using-mustache#xyz">string</a>
Run Code Online (Sandbox Code Playgroud)
需要的输出:
谢谢
sve*_*ens 11
电流输出:
Run Code Online (Sandbox Code Playgroud)<a href="http://stackoverflow.com/questions/5335873/how-to-get-html-from-json-data-using-mustache#xyz">string</a>
那不是输出.你实际得到的是类似的东西
<a href="http://stackoverflow.com/questions/5335873/how-to-get-html-from-json-data-using-mustache#xyz">string</a>
Run Code Online (Sandbox Code Playgroud)
当然,这与您在浏览器中发布的内容相似.Mustache HTML默认会转义所有变量,因此它们不会弄乱你的HTML.
在这种情况下,您不希望这样,因此您应该{{{string}}}在模板中使用.一定要只使用可信变量,不要用它来输出任何用户输入.