steo
steo

Reputation: 4656

template inside html mustache js

I'm using mustache to render portion of html. To do this , I'm using this javascript :

   var template = $("#div_name").html();
   ..
   ..
   $("#container").append(Mustace.render(template,some_object));

As the <div id="div_name"></div> still remain inside the html , I use it only to get a template i was wondering : is it convenient to keep templated div html be inside the html or is better to load it e.g. with partials through ajax ? Thanks in advance.

Upvotes: 0

Views: 1120

Answers (1)

Esailija
Esailija

Reputation: 140234

You can also keep templates inside script elements like so:

   <script type="application/mustache" id="div_name">
       //Mustache template code here
   </script>

And fetch the template code from there using $("#div_name").html(). It won't be shown on the page and the script tag is not executed because of the invalid type.

Upvotes: 3

Related Questions