2017-06-20 76 views
0

我正在使用express 4 - ejs組合爲我的web應用程序,我想知道如何渲染多個文件取決於被調用的路由/ url。快速呼叫多個文件 - EJS

比方說,我有header.ejs,我要呈現在每一頁上,並header.ejs代碼:

<div class="wrapper"> 
    <div class="header-area">Header IS Gonna Be Here</div> 
    <div class="content-area">Content Area Gonna Be Different on Every Page Depend on the Router</div> 
</div> 

我的存在調用header.ejs文件的方法是using res.render()但我不能找出它同時呈現多個文件。謝謝你的回答,如果你覺得我的問題已經被問到了,你可以給我鏈接,然後我會檢查出來。

回答

0

這個概念被稱爲您的佈局模板,基本上創建保持靜態的頁眉頁腳部分。在res.render(),你會渲染你需要的數據視圖,並添加到頁眉和頁腳:

app.get('/', function(req, res){ 
    res.render('index',{user:"John Smith"}) 
// pass main ejs file name, pass any data variables 
}); 

要渲染名爲index.ejs文件,與使用其他EJS合併文件包括語句。一定要確保index.ejs是在views目錄中已設置最初

<!DOCTYPE html> 
< html lang="en"> 
    < head> 
    <% include ../partials/head %> 
    </ head> 
    < body class="container"> 
    < header> 
     <% include ../partials/header %> 
    </ header> 
    < main> 
     < div class="jumbotron"> 
     < h1> This is great </ h1> 
     < p> Welcome to templating using EJS </ p> 
     < p> welcome <%= user%>;< /p> 
     </ div> 
    </ main> 
    < footer> 
     <% include ../partials/footer %> 
    </ footer> 
    </ body> 
</ html> 

Check for tutorial

+0

以及如何區分,包括不同的路線上的文件嗎? – myd07

+0

檢查更新的答案。 –

+0

謝謝,它的工作 – myd07