In the Node.js Web Dev book by David Herron, how does data (the input object) get to handlebars templates when I don't see it in the code anywhere

42 views Asked by At

Some templates have no code for inserting the layout template that is in most of the output. Like login.hbs does not have {{>layout}} yet layout is in all the pages in the output of the application. If this code is not there, how does the data find it's way to these templates. The only way I know to pass data is that way. I can't find the code that calls the layout.hbs template. The home route just calls index.hbs with no code for calling layout.hbs or header.hbs (partials). Code: https://github.com/PacktPublishing/Node.js-Web-Development-Fifth-Edition (use the chapter 8 branch) I have looked at possibly app.js having something in the app.set commands for hbs. There is just one command and it is setting the view engine.

1

There are 1 answers

1
Jaron Haberstroh On

I've never used "hbs" when making an express.js app so I unfortunately can't speak from experience. After some research though I think this is a default behavior of handlebars aka "hbs"

If you check this link here you'll see the "hbs.js" file, and I believe the variable on line 148 specifies the layout option if not provided in config. After which it will render your view inside the layout variable {{{body}}}.

Here's a similar stackoverflow question detailing setting up a default layout in express with "hbs" which you might find useful.