Beginners guide to use Handlebars with NodeJS app

Minimal Templating on Steroids

Introduction

Handlebars

Installation

npm init     //to initialize the project
npm install express
npm install hbs

Getting Started

|--node_modules
|--public
| |--stylesheet.css
|--routes
| |--pages.js
|--views
| |--partials
| | |--navbar.hbs
| |--index.hbs
|--app.js
|--package-lock.json
|--package.json

Handlebar Expressions

{
country: {
name: "India",
capital: "New Delhi",
},
}
country = [
{
name: "India",
capital: "New Delhi",
},
{
name: "England",
capital: "London",
},
{
name: "Russia",
capital: "Moscow",
},
]
{{#each country}}
{{@index}} : {{capital}} is the capital of {{name}}
{{/each}}
if statement
{{#if value}}
//Do something
{{/if}}
if-else statement
{{#if value}}
//Do something
{{else}}
//Do something
{{/if}}

Partials

{{> partial parameter1=value1 parameter2=value2}}

Custom Helpers

Conclusion

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store