Curious to Knowing The Best Way To Use Handlebars Partials Based On Express Render Variables

StackOverflow https://stackoverflow.com/questions/22623949

سؤال

So I'm wondering what is a best way to load a Handlebars Partial in Express when I Have a Render Variable set.

app.get('/raves-reviews/corporate-clients', function (req, res) {
    res.render('raves-reviews.html', {  //This of this a micro layout
        layout: 'main',           //this is the main layout
        title: 'Raves & Reviews',
        corporate: true,          //this is part of idea 1
        page: 'corporate-clients' //this is part of idea 2 and 3
    });
});

So when a user goes to mysite.com/raves-reviews/corporate-clients, Handlebars will first load the main layout, which I have setup with the navigation to show either the home page slider, or the raves-review slider if its not the home page.

Then it'll load the raves-reviews.html page, which is just a handlebars file that I've renamed the extension to .html, that will be a micro template that will house 4 other pages.

What I don't want to do is have to make 4 pages, with the same raves-reviews.html micro template and have to include each of the other pages as partials. I'd rather make 1 page that will be able to handle every partial it needs like the main layout does with every other page in the website.

So idea 1, is creating 4 {{ #if }} blocks in raves-reviews.html

{{#if page1}}
    {{> page1}}
{{/if}}
{{#if page2}}
    {{> page2}}
{{/if}}
{{#if page3}}
    {{> page3}}
{{/if}}
{{#if page4}}
    {{> page41}}
{{/if}}

So idea 2 was to use a helper that I found here: Logical operator in a handlebars.js {{#if}} conditional to create the pages, however I did read there that handlebars was suppose to be free of logic. So I'm not really sure if this would even be a good method invoking logic into a templating system that was designed to be used without logic.

Idea 3 was to see if there was a way to use the page variable in app.get to create a partial, but I'm not 100% sure how to pull it off. My original idea was to do:

{{> {{page}} }}

If there is any other ways I'm totally open to any other ideas. Thanks everyone.

هل كانت مفيدة؟

المحلول

you should probably go with option 1, simply because you already did it

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top