droidbot
droidbot

Reputation: 997

Using handlebars runtime library

Can somebody help me with a sample code on how to use the handlebars runtime library (handlebars.runtime.js) please?

I have tried to use Handlebars.compile, which obviously does not work, because I know runtime library is to avoid compiling templates.

Also I have tried to use Handlebars.template method by passing the template as a string, but it is not working as the template method expects a function as a parameter.

I think I am doing something basically wrong. Are there any documentations on how to use runtime library on its own?

Much appreciate the help.

More details: I first used handlebars.js file, which was working good, but my team-mate noticed the compressed file is too big (>40KB) for our purpose (just a few templates in our site).

So, I tried using just handlebars.runtime.js file. Is this correct, or am I missing something?

Here is the sample I have tried: http://jsfiddle.net/2KfsM/

<div id="container"></div>
<script id="hb-example" type="text/handlebars-template">
  <p>{{sampleText}}</p>
</script>

The js piece:

var template = Handlebars.compile($('#hb-example').html());
$('#container').html(template({sampleText: 'Here is a sample'}));

Upvotes: 1

Views: 2982

Answers (1)

Shibbir Ahmed
Shibbir Ahmed

Reputation: 1350

You can really improve performance by precompiling your template. But, in order to use the handlebar runtime library you have to first compile your existing template.

There is no Handlebars.compile function available in the runtime library.

Here is some links:

http://handlebarsjs.com/precompilation.html

http://berzniz.com/post/24743062344/handling-handlebars-js-like-a-pro

Upvotes: 2

Related Questions