【问题标题】:layout customisation vs using two layouts - expressjs布局定制与使用两种布局 - expressjs
【发布时间】:2018-04-08 00:43:18
【问题描述】:

假设我有一个主要的layout.handlebars 和一个包含两个主要部分的网站。 如果我像这样自定义layout.handlebars,会有什么性能差异:

<head>
 <title>{{title}}</title>

 <link href="css/bootstrap.css" rel="stylesheet">
 {{#if section1}}
 <link href="css/section1.css" rel="stylesheet">
 {{else}}
 <link href="css/section2.css" rel="stylesheet">
 {{/if}}

</head>

并像这样渲染:

router.get('/section1', function(req, res){
    res.render('section1',{title: 'Section 1', section1: true});
});

而不是为网站的两个部分分别使用两种不同的布局?

【问题讨论】:

    标签: html node.js express express-handlebars


    【解决方案1】:

    不会影响性能,但您最终会得到凌乱的代码。
    如果出现“第 3 节”怎么办?另一个如果?

    怎么样

    <head>
     <title>{{title}}</title>
    
     <link href="css/bootstrap.css" rel="stylesheet">
     <link href="css/section1.css" rel="stylesheet">
     <link href="css/section{{section}}.css" rel="stylesheet">
    </head>
    

    渲染如下:

    router.get('/section1', function(req, res){
        res.render('section1',{title: 'Section 1', section: "1"});
    });
    

    router.get('/section2', function(req, res){
        res.render('section2',{title: 'Section 2', section: "2"});
    });
    

    【讨论】:

    • 这是一个很好的解决方案。但是如果第 3 节出现并且每个节都有多个 css/js 文件怎么办? if/else if 语句不是更快吗?还是归结为正确命名文件?顺便说一句,假设存在 else if 语句,因为据我所知它不存在。
    • 捆绑你的 js / css 文件。这将是be faster 思路的更多关注。不要忘记,这些 if / else 块和/或我的解决方案是在服务器端呈现的 - 就性能而言,它实际上什么都没有
    • 另一个选项是 Partials - handlebarsjs.com/partials.html
    猜你喜欢
    • 2015-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多