簡體   English   中英

在一個布局上使用Express-Partials的多個Partial

[英]Multiple Partials Using Express-Partials On One Layout

我正在使用Node.js和ExpressJS 3.0。 轉到3.0,我發現不再支持局部函數,隨后發現Express-Partials提供了類似的功能。

查看GitHub頁面上的示例,我發現:

app.get('/',function(req,res,next){
  res.render('index.ejs') 
  // -> render layout.ejs with index.ejs as `body`.
})

很好,但是如果我的布局依賴於多個局部元素怎么辦? 也就是說,如果我在layout.ejs文件中有一個塊用於標題,一個用於內容,一個用於頁腳,該怎么辦? 例如,如果我對整個Web應用程序使用一個模板文件,但是不同類型的用戶具有不同的頁眉,內容塊和頁腳怎么辦?

查看express-partials的有限文檔,我找不到此功能。 我期待這樣的事情:

app.get('/', function (req, res) {
   res.render({header: 'header1.ejs', content: 'some_file.ejs', footer: 'footer1.ejs'});
   // -> render layout.ejs with header1.ejs as `header`, some_file.ejs as `content, and footer.ejs as `footer
});

如何實現此功能?

我建議您使用ejs包含+開關

抱歉,我不熟悉ejs語法,所以– jade,但本質上是相同的:

app.get('/', function (req, res) {
   res.render('index', {
       header: 'header1', 
       , content: 'content1', 
       , footer: 'footer1'
    });
});

index.jade
===========
//- header
case header
  when "header1":
    include "includes/header1"
  when "header2":
    include "includes/header2"
...
case content
  when "content1":
    include "includes/some_file1"
  when "content2":
    include "includes/some_file2"
....

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM