如何从JSON数据中获取HTML,使用MUSTACHE?

时间:2021-11-21 14:30:29

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.

我在我的新网站中使用JSON和MUSTACHE作为模板。但我不知道如何从json数据中获取HTML。我在后端使用PHP,它作为API提供者工作。我对这个概念很新。 Evey的帮助和建议是欢迎的。

Thanks.

谢谢。

Code I am using::

我正在使用的代码::

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

JSON data is like::

JSON数据就像::

{"menu": {
  "id": "file",
  "string": "<a href=\"http:\/\/*.com\/questions\/5335873\/how-to-get-html-from-json-data-using-mustache#xyz\">string</a>",
}}

MUSTACHE template:

MUSTACHE模板:

{{#string}}
<div>
{{string}}
</div>
{{/string}}

Current Output:

电流输出:

<a href="https://*.com/questions/5335873/how-to-get-html-from-json-data-using-mustache#xyz">string</a>

Output Needed:

需要的输出:

string

Thanks you

谢谢

2 个解决方案

#1


11  

Current Output:

电流输出:

<a href="http://*.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

那不是输出。你实际得到的是类似的东西

&lt;a href=&quot;http://*.com/questions/5335873/how-to-get-html-from-json-data-using-mustache#xyz&quot;&gt;string&lt;/a&gt;

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.

当然,这与您在浏览器中发布的内容类似。 Mustache HTML默认会转义所有变量,因此它们不会弄乱你的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.

在这种情况下,您不希望这样,因此您应该在模板中使用{{{string}}}。一定要只使用可信变量,不要用它来输出任何用户输入。

#2


0  

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:

看看PHP中的json_decode,它将为您提供一系列您(我猜想)可以提供给您的模板引擎的数据:

$json = <<< EOJ 
{"menu": {
  "id": "file",
  "string": "<a href=\"http:\/\/*.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);

#1


11  

Current Output:

电流输出:

<a href="http://*.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

那不是输出。你实际得到的是类似的东西

&lt;a href=&quot;http://*.com/questions/5335873/how-to-get-html-from-json-data-using-mustache#xyz&quot;&gt;string&lt;/a&gt;

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.

当然,这与您在浏览器中发布的内容类似。 Mustache HTML默认会转义所有变量,因此它们不会弄乱你的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.

在这种情况下,您不希望这样,因此您应该在模板中使用{{{string}}}。一定要只使用可信变量,不要用它来输出任何用户输入。

#2


0  

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:

看看PHP中的json_decode,它将为您提供一系列您(我猜想)可以提供给您的模板引擎的数据:

$json = <<< EOJ 
{"menu": {
  "id": "file",
  "string": "<a href=\"http:\/\/*.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);