After attending a wedding, I got interested in the idea of templating. When you send an invitation, all of the information remains the same for each recipient with the exception of the “to:” portion. The body of your invitation is identical, but there is a greeting addressed to each individual. The ability to change this individual greeting dynamically is an example of templating.

New programmers are constantly taught to keep their code DRY, meaning ‘Don’t Repeat Yourself’. Better code is dynamic and reusable. There are a variety of methods that allow for templating, but I will discuss one related to Node, as it has been my most recent blogging topic.

Handlebars is a popular templating engine and JavaScript library, which pairs nicely with an Express server. To get started, download the hbs package on NPM.

Once installed, you need a single line of code to begin. Write app.set(“view engine”, “hbs”). This tells Express the templating engine has been installed and is ready for use.

But instead of serving a static document through the public folder, you will be rendering dynamic pages, which must live instead inside a “views” directory inside the root folder.

#javascript #coding #programming #nodejs

Node Templating: Handlebars for Express
1.30 GEEK