简体   繁体   English

很想知道基于快速渲染变量使用把手部分的最佳方法

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

So I'm wondering what is a best way to load a Handlebars Partial in Express when I Have a Render Variable set. 所以我想知道当我有一个渲染变量集时,在Express中加载Handlebars Partial的最佳方法是什么。

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. 因此,当用户访问mysite.com/raves-reviews/corporate-clients时,Handlebars将首先加载主布局,我已设置导航以显示主页滑块或raves-review滑块(如果不是主页。

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. 然后它会加载raves-reviews.html页面,这只是一个把我重命名为.html扩展名的把手文件,它将是一个可容纳4个其他页面的微模板。

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. 我不想做的是使用相同的raves-reviews.html微模板制作4页,并且必须将每个其他页面包括为部分。 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 因此,想法1是在raves-reviews.html中创建4个{{#if}}块

{{#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. 因此,想法2是使用我在这里找到的帮助器: handlebars.js {{#if}}中的逻辑运算符有条件地创建页面,但是我确实在那里读到把手被认为没有逻辑。 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. 想法3是看是否有办法在app.get中使用页面变量来创建局部,但我不是100%确定如何将它拉下来。 My original idea was to do: 我最初的想法是:

{{> {{page}} }}

If there is any other ways I'm totally open to any other ideas. 如果有任何其他方式,我对任何其他想法完全开放。 Thanks everyone. 感谢大家。

你可能应该选择1,因为你已经做到了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM