1

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'}));
droidbot
  • 737
  • 1
  • 5
  • 26

1 Answers1

2

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

Shibbir Ahmed
  • 1,327
  • 14
  • 19
  • 1
    Thanks. Found this one, which gives better details: http://stackoverflow.com/questions/8659787/using-pre-compiled-templates-with-handlebars-js-jquery-mobile-environment – droidbot Jan 22 '14 at 13:05
  • 1
    wondering why there is no **good** documentation about precompilation in handlebarsjs.com – droidbot Jan 22 '14 at 13:20