22

Pardon the noob question but I simply can't get precompiled Handlebars templates to do anything but barf out

TypeError: 'undefined' is not a function (evaluating 'templateSpec.call(container, Handlebars, context, options.helpers, options.partials, options.data)')

each time I apply a context to a (precompiled) template.

Given the following files and contents:

  • hello.handlebars: <p>Hello, {{name}}</p>
  • templates.js: the result of compiling hello.handlebars via handlebars hello.handlebars -f templates.js
  • index.html: includes Handlebars RC1 runtime in the head and this for the body:

    <body id="body">
      <script src="templates.js" type="text/javascript" charset="utf-8"></script>
      <script type="text/javascript" charset="utf-8">
        var compiledTemplate = Handlebars.templates['hello'];
        var html = compiledTemplate({ name: 'World' });
        document.getElementById('body').innerHTML = html;
      </script>
    </body>
    

Calling compiledTemplate() throws that error above no matter what I do--yet I'm able to get client-side templates to compile and display just fine. All the walkthroughs and tutorials I've seen skip through this like its obvious so I must be missing something silly. Any ideas?

ele
  • 6,021
  • 5
  • 25
  • 35

2 Answers2

27

Be sure to match the versions of the Handlebars server and client packages. A way to check the version of the compiled template is the following:

handlebars a_template_of_yours.handlebars | grep "compilerInfo"

It should output something like this:

this.compilerInfo = [4,'>= 1.0.0'];

The first value is the Compiler Revision, which should match the output of the following code on your browser:

Handlebars.COMPILER_REVISION

If you used npm to download your Handlebars compiler, specify the version which you need. Here is an example for handlebars.runtime-v1.3.0.js:

npm install handlebars@1.3.0 -g

biomorgoth
  • 630
  • 4
  • 8
  • 7
    This helped me. npm installed per default `handlebars-2.0.0-alpha`... which was not compatible to the runtime 1.3.0 – otmezger Mar 12 '14 at 19:16
  • I was running [the 1.0.rc.1](https://github.com/wycats/handlebars.js/downloads) in both cases. – ele Mar 13 '14 at 02:49
  • @ele, i would recommend you -if possible- to upgrade to a more recent version of Handlebars, since 1.0.rc.1 dates from September 2012. The latest stable version is 1.3.0 from January 2014, so there might be a lot of improvements you will also be missing. You can download the latest stable client (i.e. the runtime js module) from [Handlebars Home](http://handlebarsjs.com/). The node compiler (server module) can be downloaded from npm (specifying the version, to prevent downloading some new alpha version), or from the [Github releases site](https://github.com/wycats/handlebars.js/releases). – biomorgoth Mar 13 '14 at 21:38
  • 1
    `handlebars | grep "compilerInfo"` returned me anything. however, `handlebars | grep "compiler"` returned me `,"...compiler":[6,">= 2.0.0-beta.1"],"main":function(depth0,helpers,partials,data,depths...` so it is 2.0.0 ? how that value came from a non-compiled template? I can't understand that. – zok Oct 10 '14 at 20:49
  • This comment is old, but i'm answering for the record. Probably naming changed in that version of handlebars. Yours seems to be `2.0.0-beta.1`. That value came from the compiled template you are generating through executing the handlebars compiler with `handlebars `. Later, the `grep "compiler"` looks for the text "compiler" in the output generated by the handlebars compiler, thanks to the pipeline. – biomorgoth Apr 26 '15 at 07:25
3

The problem for me is that currently (June 2014), the 2.0 release on GitHub is marked alpha https://github.com/wycats/handlebars.js/releases.

If I install handlebars via bower, it selects the latest stable release. v1.3.0.

npm install handlebars, however, installs the ~2.0.0-alpha.4 version.

Solution: update the version in bower.json to ~2.0.*, and run bower update.

Tim
  • 6,281
  • 3
  • 39
  • 49