2017-10-26 17 views
1

私は1つのメインlayout.handlebarsと2つのメインセクションを持つウェブサイトを持っているとしましょう。レイアウトのカスタマイズと2つのレイアウトを使用する - expressjs

<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}); 
}); 

代わりのウェブサイトの両方のセクションの各二つの異なるレイアウトを使用して、私はこのようlayout.handlebarsをカスタマイズした場合 は、任意のパフォーマンスの違いはありますでしょうか?

答えて

1

パフォーマンスは低下しますが、乱雑なコードになります。
「セクション3」があればどうなりますか?別の場合は?良い解決策だ

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"}); 
}); 
+0

<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> 

についてそれをのようなものをレンダリング何

。しかし、もしセクション3が出てきて、各セクションに複数のCSS/jsファイルがあればどうでしょうか? 'if/else if'文の方が高速でしょうか?それとも、すべてのファイルに適切な名前を付けるのだろうか? Btw、 'else if'文が存在すると仮定すると、私の知る限りではありません。 – gitterio

+0

js/cssファイルをバンドルします。それは思考の「より速い」訓練の懸念の多くである。これらの 'if/else'ブロックおよび/または私の解決策がサーバー側にレンダリングされることを忘れないでください。パフォーマンス面で文字通り何もありません。 – Alex

+0

別のオプションは、Partials - http://handlebarsjs.com/partialsです。 html – Alex

関連する問題