Handlebars for Beginners

handlebars-for-beginners

What is Handlebars?

Handlebars is one of the most used templating engines for web applications “competing” with other well-known ones like Mustache js, Pug, EJS and others. It’s especially used on the server side along with the express js framework.

Official Documentation:- Handlebars

Folder Structure Image:-

Folder Structure

Package.json Structure:-

Package.json Image

App.js Code:-

const express = require("express");
const handlebars = require('express-handlebars');
const path = require("path");
const helpers = require("handlebars-helpers")();

const app = express();
const PORT = 8000;

const hbs = handlebars.create({
    extname: "hbs",
    defaultLayout: "main",
    layoutsDir: path.join(__dirname, 'views/layout'),
    helpers: helpers,
    partialsDir: path.join(__dirname, 'views/partials')
});

app.engine("hbs", hbs.engine);
app.set('view engine', 'hbs');
app.set('views', path.join(__dirname, "views"));

// Custom Helper 
helpers.makeBold = function (aString) {
    return '' + aString.toUpperCase() + '';
};


app.get("/", (req, res) => {
    res.render("home", {
        title: "Express",
        peoples: ["salman", "sharukh", "amitabh"],
        products: [
            {
                name: "mobile",
                category: "Electronics",
                stock: 1000
            },
            {
                name: "Electronics",
                category: "Electronics",
                stock: 0
            },
            {
                name: "mobile",
                category: "Electronics",
                stock: 1000
            }
        ]
    })
})
app.get("/about", (req, res) => {
    res.render("about", {
        title: "about",
        layout: "second"
    });
})


app.listen(PORT, () => {
    console.log(`Server is Listening at ${PORT} Port.`);
})

main.hbs Code:


 lang="en">
    
         charset="UTF-8">
         http-equiv="X-UA-Compatible" content="IE=edge">
         name="viewport" content="width=device-width, initial-scale=1.0">
        </span>{{title}}<span class="nt">
    
    
        

home

{{{body}}}

second.hbs:-


 lang="en">
    
         charset="UTF-8">
         http-equiv="X-UA-Compatible" content="IE=edge">
         name="viewport" content="width=device-width, initial-scale=1.0">
        </span>{{title}}<span class="nt">
    
    
        

second

{{{body}}}

productloop.hbs Code:-


products - {{#each products}} style="color: red;"> {{@index}} - {{this.name}} {{#eq this.name "mobile"}} common {{else}} not common {{/eq}} {{#if this.stock}} {{this.stock}} {{else}} (zero) {{/if}} {{/each}}


home.hbs code:-

home - {{title}}

peoples - {{peoples}}

{{add 20 30}}

divide {{floor (divide 20 12)}}

{{!-- {{/add}} --}}

makebold {{#makeBold "aksh"}} {{/makeBold}}

{{#contains peoples "salman1"}} salman is there {{else}} salman is not there {{/contains}}

peoples - {{#each peoples}} {{this}} {{/each}}


products - {{#each products}} style="color: red;"> {{@index}} - {{this.name}} {{#eq this.name "mobile"}} common {{else}} not common {{/eq}} {{#if this.stock}} {{this.stock}} {{else}} (zero) {{/if}} {{/each}}


{{> productloop}}

about.hbs code:-

About - {{title}}

Output Image of Home Page:-

Output Image

Output Image of About Page:-

Output Image of About Page

Thank You.
You can follow us on:
Youtube
Instagram

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
aws-guide-for-beginners

AWS Guide for Beginners

Next Post
aks-security:-10-proven-tactics-for-securing-your-kubernetes-clusters

AKS Security: 10 Proven Tactics for Securing Your Kubernetes Clusters

Related Posts