1

I'm new to node js and came across this hbs module and saw it in this part code for example :

app.set('view engine', 'html');
app.engine('html', require('hbs').__express);

can anyone please explain what is hbs (handlebars - but what does it do)? and why the second line is needed if the first already says that the files will be opened as html

Thank you!

asasdasd
  • 33
  • 1
  • 6

1 Answers1

8

hbs is a express.js wrapper for the handlebars.js javascript template engine. Handlebars.js is a template engine to make writing html code easier, if intrested you can look here. But handlebars.js is meant for client-side copilation(the browser compiles the templates) so you need a wrapper like hbs.

A wrapper makes it possible to use for example a client-side library in express.js and that is what hbs does. This was a little simplify, but it explains the principle.

Over to your second question, why the second line is needed. and that is because if you use the standard line:

app.set('view engine', 'hbs');

express.js looks for the view engine named hbs, but in your example:

app.set('view engine', 'html');
app.engine('html', require('hbs').__express);

express.js dosent know what to look for in case of the view engine defined as html and you have to define this view engine in the second line, so express.js knows what to look for. If you look here, you can see that it says, Express loads it internally.

NatureShade
  • 2,187
  • 1
  • 19
  • 27
  • Thanks for your answer . so the second line specify which wrapper to use? and for example when i write app.set('view engine', 'jade'); do i write also something like this, i saw an example that didnt do it with jade – asasdasd Dec 07 '14 at 13:37
  • No, you don’t need to do that with *jade* because then jade has already told express.js what to look for in case of *view engine* defined as *jade* – NatureShade Dec 07 '14 at 13:39