snipt

Ctrl+h for KB shortcuts

JavaScript

Parse JSON with Mustache and jQuery

1
2
3
4
5
$.getJSON('json/data.json', function(data) {
    var template = "<h1>{{firstName}} {{lastName}}</h1>Blog: {{blogURL}}";
    var html = Mustache.to_html(template, data);
    $('#sampleArea').html(html);
});
https://snipt.net/embed/59770f3241dc55ccac435ce7a3f72715/
/raw/59770f3241dc55ccac435ce7a3f72715/
59770f3241dc55ccac435ce7a3f72715
js
JavaScript
5
2019-07-14T06:18:53
True
False
False
Nov 11, 2012 at 11:25 PM
/api/public/snipt/50051/
parse-json-with-mustache-and-jquery-1
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="nx">$</span><span class="p">.</span><span class="nx">getJSON</span><span class="p">(</span><span class="s1">&#39;json/data.json&#39;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">data</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-2"><a name="L-2"></a> <span class="kd">var</span> <span class="nx">template</span> <span class="o">=</span> <span class="s2">&quot;&lt;h1&gt;{{firstName}} {{lastName}}&lt;/h1&gt;Blog: {{blogURL}}&quot;</span><span class="p">;</span> </span><span id="L-3"><a name="L-3"></a> <span class="kd">var</span> <span class="nx">html</span> <span class="o">=</span> <span class="nx">Mustache</span><span class="p">.</span><span class="nx">to_html</span><span class="p">(</span><span class="nx">template</span><span class="p">,</span> <span class="nx">data</span><span class="p">);</span> </span><span id="L-4"><a name="L-4"></a> <span class="nx">$</span><span class="p">(</span><span class="s1">&#39;#sampleArea&#39;</span><span class="p">).</span><span class="nx">html</span><span class="p">(</span><span class="nx">html</span><span class="p">);</span> </span><span id="L-5"><a name="L-5"></a><span class="p">});</span> </span></pre></div> </td></tr></table>
Mustache, jQuery