Monday, 10 April 2017

How to restrict Handlebars templating to run on certain pages

I have a few pages, e.g. '/products', '/contact-us', '/blog' which I have full html pages I want to display.

However I also have another few pages under '/demo/dashboard', '/demo/devices', '/demo/summary' that I'm intending to use handlebars templating for. How do I restrict my handlebars templating to only be applied to all the endpoints after '/demo'?

The funny thing is when I ran it on my windows machine, everything is fine but when I loaded it onto my server, the templating seemed to be 'forced' on all the pages I have. Nothing I've tried seems to work, appreciate if anyone can point me to the right direction, thanks!

My Node.JS code is as follows:

app.get('/products', function(req,res){
    res.sendFile(__dirname + '/public/products.html);
});

app.get('contact-us', function(req,res){
    res.sendFile(__dirname + '/public/contact-us.html);
});

app.get('blog', function(req,res){
    res.sendFile(__dirname + '/public/blog.html);
});

app.set("view engine", "handlebars");

app.engine("handlebars", handlebars({
    defaultLayout: "main",
    helpers: {
        isActive: function(view,actual){
            if (view == actual)
                return("active");
            return("");
        }
    }
}));

app.get('/demo/:view', function(req,res){
    var page = req.params.view;
    console.log("In page: %s", page);
    res.render(page);
});



via jlyh

No comments:

Post a Comment