2011-03-17 5 views
6

मैं अपनी नई साइट में टेम्पलेट्स के लिए JSON और MUSTACHE का उपयोग कर रहा हूं। लेकिन मुझे नहीं पता कि मैं जेएसएम डेटा से एचटीएमएल कैसे प्राप्त कर सकता हूं। मैं बैकएंड में PHP का उपयोग कर रहा हूं, जो एपीआई प्रदाता के रूप में काम कर रहा है। मैं इस अवधारणा के लिए बहुत नया हूँ। एवे मदद और सुझाव स्वागत है।मस्टेच का उपयोग करके JSON डेटा से HTML कैसे प्राप्त करें?

धन्यवाद।

कोड मैं उपयोग कर रहा हूँ ::

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

आउटपुट आवश्यक:

string

आप धन्यवाद

उत्तर

11

वर्तमान आउटपुट:

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

जो कुछ भी ब्राउज़र में आपने पोस्ट किया है, वैसे ही दिखता है। मूंछ एचटीएमएल डिफ़ॉल्ट रूप से आपके सभी चर से बच निकलता है, इसलिए वे आपके एचटीएमएल को गड़बड़ नहीं करते हैं।

इस मामले में आप यह नहीं चाहते हैं, इसलिए आपको टेम्पलेट में {{{string}}} का उपयोग करना चाहिए। केवल भरोसेमंद चर के साथ ऐसा करना सुनिश्चित करें, किसी भी उपयोगकर्ता इनपुट को आउटपुट करने के लिए इसका कभी भी उपयोग न करें।

0

PHP में json_decode पर एक नजर डालें, कि आप अपने डेटा जो आप (मैं अनुमान) की एक सरणी मिल जाएगा अपने templating फ़ीड कर सकते हैं इंजन:

$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); 
+0

मुझे खेद है, मुझे लगता है कि मैं इसे अच्छी तरह से समझाने में सक्षम नहीं था, असल में मैं PHP को मान वापस नहीं दे रहा हूं, लेकिन इससे मूल्य प्राप्त कर रहा हूं .. मैंने बेहतर समझने के लिए प्रश्न संपादित किया है .. उत्तर के लिए धन्यवाद। अधिक मदद के लिए तत्पर हैं –

संबंधित मुद्दे