hogan templates defined in html (script or div element) instead of inline javascript

1.7k views Asked by At

I would like to swap our template engine from ICanHas (Mustache) to Hogan. ICanHas allows you to define templates in a script block. Is it possible to do this with Hogan?

1

There are 1 answers

1
maxbeatty On BEST ANSWER

Yes. Here's a simple template in a script tag:

<script id="hogan-tpl" type="text">
    hello {{planet}}
</script>​

Here's the minimal JavaScript needed that grabs the template from the script element, compiles it with Hogan.js, and renders with the appropriate data before assigning it to the innerHTML of the body:

var template = $('#hogan-tpl').html(),
    hello = Hogan.compile(template),
    context = { planet: "world" },
    tpl = hello.render(context);

document.body.innerHTML = tpl;​

Here is it working on jsFiddle