Handlebars in NodeJS

"use strict";
 
(function() {
    const handlebars = require("handlebars");
    const source = "{{title}}\n"
        + "Hello {{person.name}}, I see that you are {{person.age}} years old.\n"
        + "You have the following cars:\n"
        + "{{#each cars}}\n"
        + "    {{year}} {{make}} {{model}} {{#if color}}(Color={{color}}){{/if}}\n"
        + "{{/each}}\n";
 
    const template = handlebars.compile(source);
    const data = {
        title: "My Data Object",
        person: { name: "Billy", age: 34 },
        cars: [
            { year: 2015, make: "Ford", model: "Mustang", color: "Red" },
            { year: 2014, make: "Chevy", model: "Corvette" },
            { year: 2013, make: "Dodge", model: "Aries", color: "Yellow" }
        ]
    };
    const result = template(data);
    console.log(result);
})();
 
/****** OUTPUT *******
My Data Object
Hello Billy, I see that you are 34 years old.
You have the following cars:
    2015 Ford Mustang (Color=Red)
    2014 Chevy Corvette
    2013 Dodge Aries (Color=Yellow)
******* OUTPUT ******/

Comments

Popular posts from this blog

C# Record Serialization

Read/write large blob to SQL Server from C#

Add timestamp to photo using ImageMagick