Getting Started With Express 4.x – Express Handlebar – Tutorial 4

In this blog post we will see more advanced usage of handlebar template engine with express

In our previous tutorial we saw how to create a helloworld application using handlebar with express, here we will see more advanced usage of handlebar.


We have seen already how to pass variables in our route files, now to access those variables in our view files is easy




If Condition/Each Loop with Handlebar

If conditions and for loops are quite easy


{{#if variable}}

//if true


//if flase


{{#each array_variable}}



//if array empty



Handlebar Helpers

Most of the time we require advance logic in our templates, not just simple if and for loops. For those instances we use helpers. Through helpers we can write javascript functions and call them inside templates.

Below we have created an helper function called “canDisplayDeal”. The function is defined in index.js routes file and is called in the handlebar template.

More details about helpers

The entire code base for this is available here

1. Implement the bootstrap starter template using handlebars (
2. Create an helper function using the above “deals” json object. If the string length “name” in deals is greater than 10, add … and truncate the name else output it as it is.