Blog

Using express and express-handlebars Modules to Object Properties to a Handlebar Template
Posted on June 20, 2016 in Node.js by Matt Jennings

/server.js Example Code

app.get('/tester', function(req, res) {
    // Below I'm passing in a JSON object (or hash table of keys and values, which is similar to an associative array) 
    // as a 2nd parameter to the "tester.handlebars" template
    res.render('tester', {
        first_name: "Donald",
        last_name: "Duck",
        now: new Date(),
        random_num: Math.round(Math.random() * 10)
    });
});

/views/tester.handlebars Example Code

<h3>This is Tester page</h3>

{{!-- Property value pulled from server.js --}}
{{last_name}}, {{first_name}}

<p>Your lucky number is {{random_num}} on {{now}}</p>

Leave a Reply