javascripticanhaz.js

How can i load javascript templates with their ids?


Been playing around some with iCanHaz.js templates which i define in the head of my html pages like this.

<script id="test" type="text/html">
<h1>Test</h1>
</script>

I do want to have all my iCanHaz templates in a single file and i dont know how i should import them.

Thanks in advance!


Solution

  • They provide an example for how to pull templates from a server on their site, almost at the bottom, if you use jQuery as well:

    $.getJSON('/myserver/templates.json', function (templates) {
        $.each(templates, function (template) {
            ich.addTemplate(template.name, template.template);
        });
    });
    

    Then of course you would have to have some server-side code that provide the templates as JSON, and how you do that depends on what server-side language you are using, as pointed out by Poelinca Dorin in his comment.