Nippysaurus
Nippysaurus

Reputation: 20378

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

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?

Upvotes: 0

Views: 1729

Answers (1)

maxbeatty
maxbeatty

Reputation: 9325

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

Upvotes: 3

Related Questions