Search code examples
backbone.jshandlebars.js

Handlebars template's filename extension


I changed my handlebar template's extension and referred to the same in the function which called handlebarjs' compile function.

It worked perfectly fine with no issues.

But I'm curious to know if anyone else tried that? Please let me know if you think this could cause problems down the road for any reason.

For some reason I feel that the very extension .handlebars is a bit long. I prefer to keep it to a max of 4 chars ... something like .txt or .html.

Please let me know if you see any issues with this approach.

For example, I renamed login.handlebars to login.html

In the getTemplate function (as shown below), I will call this template for compilation

function getTemplate(name) {

if (Handlebars.templates === undefined || Handlebars.templates[name] === undefined) {
    $.ajax({
       url : "templates/" + name + ".html",
       success : function(data) {
       if (Handlebars.templates === undefined) {
           Handlebars.templates = {};
       }
       Handlebars.templates[name] = Handlebars.compile(data);
       },
      async : false
    });
    }
    return Handlebars.templates[name];
}

Solution

  • My shop uses .handlebars, along with Require.js and Alex Sexton's require-handlebars plug-in, and it all works without issue. The far more common suffix however, and the default one in that plug-in I just mentioned, is .hbs (presumably because .hbs is a 3-character extension not already taken by another file type).

    You can for example use .hbs, .handlebars, or even a different extension for that matter, and it should work just fine with any sort of library (eg. Require) where the suffix could actually matter. There are no guarantees of course, but because there is no official extension library authors generally know better than to hard-code one.

    I would caution against using .htm or .html for these files though ... unless you have a really picky IDE. Most IDEs can be set to treat .hbs as if it were an HTML files, for syntax coloring and what not. If your's can't, then .htm might make sense. Otherwise I'd keep the file extension distinct, so that you can easily distinguish between the two types of files.