Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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]; } 
like image 695
eshcol Avatar asked Jan 17 '14 21:01

eshcol


People also ask

What are Handlebars files?

Handlebars is a simple templating language. It uses a template and an input object to generate HTML or other text formats. Handlebars templates look like regular text with embedded Handlebars expressions.

What is the HBS extension?

An HBS file is a template file created by Handlebars, a web template system. It contains a template written in HTML code and embedded with Handlebars expressions. An HBS file performs the same function as a . HANDLEBARS file.

What should be the extension for the separate template file in Handlebars?

hbs is one of the appropriate extension for handlebars templates, as seen here : github.com/github/linguist/blob/master/lib/linguist/…

How do I open a HBS file?

Installation of hbs module: You can visit the link Install hbs module. You can install this package by using this command. After installing hbs module, you can check your hbs version in command prompt using the command. After that, you can just create a folder and add a file for example index.


1 Answers

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.

like image 195
machineghost Avatar answered Oct 06 '22 11:10

machineghost