6

新しいサイトのテンプレートに JSON と MUSTACHE を使用しています。しかし、json データから HTML を取得する方法がわかりません。APIプロバイダーとして機能するバックエンドでPHPを使用しています。私はこの概念に非常に慣れていません。すべてのヘルプと提案は大歓迎です。

ありがとう。

私が使用しているコード::

<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 データは次のようになります::

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

口ひげのテンプレート:

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

現在の出力:

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

必要な出力:

ストリング

ありがとうございます

4

2 に答える 2

12

現在の出力:

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

それは出力ではありません。実際に得られるのは次のようなものです

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

もちろん、これはブラウザに投稿したもののように見えます。Mustache HTML は、デフォルトですべての変数をエスケープするため、HTML が混乱することはありません。

この場合、それは望ましくないため{{{string}}}、テンプレートで使用する必要があります。信頼できる変数でのみ行うようにしてください。ユーザー入力を出力するために使用しないでください。

于 2011-04-28T08:49:49.707 に答える
0

PHP のjson_decodeを見てみましょう。これにより、テンプレート エンジンにフィードできるデータの配列が得られます。

$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);
于 2011-03-17T08:03:18.927 に答える