I'm using Mustache.js to render different recurring parts of an HTML site. Until now I have every template inside a <script />
and access its contents through jQuerys html()
method:
<script type="text/html" id="html-script">
<!-- ... -->
</script>
<script type="text/javascript">
$('script[type="text/html"]').each(function() {
var $this = $(this);
templates[$this.attr('title')] = $this.html();
});
</script>
Now I would like to put each template in its own file and include them like this:
<script type="text/html" id="html-script" src="template.html"></script>
This does not work so my question is
- Why not?
- How can get this to work?
I read an article about How to Load Mustache.js Templates From an External File with jQuery which I could use as a fallback solution but I would really appreciate if I not had to $.get()
the templates myself.