nodejs express template (模版)的使用 (ejs + express)

來源:互聯網
上載者:User

標籤:http   os   使用   io   檔案   ar   div   cti   line   

123456789 var app=require("express").createServer();                                                                                                                                                                                                                                                                                                                                                                                      app.set("view engine","ejs");                                                                                                                                                                                                                                                                                                                                                                                      app.get("/",function(req,res){                                                                                                                                res.render("index",{"title":"test"});                                                                                                                            });                                                                                                                                                                                                                                                                                                                                                                                      app.listen(3000);

1、上面是express使用模版的一個基本例子

app.set("view engine","ejs"); 這句將模版引擎設定為 ejs (http://github.com/visionmedia/ejs)

ejs可通過 npm install ejs 來進行安裝

2、在app.get中。

res.render("index",{"title":"test"}); 會執行2個步驟。

  1. 會讀取 ./views/index.ejs檔案的內容,然後將其中的title變數替換為test,例如<%=title%>會變為test。假設返回的內容為content1

  2. 接著,會讀取./views/layout.ejs,並將其中的body變數替換為content1,例如<%=body%>會變為content1的內容。

3、在2中,如果不願意使用預設的layout.ejs,可自行指定。例如:

1 res.render("index",{"title":"test","layout":"main"});

1 res.render("index",{"title":"test","layout":"main.ejs"});

4、如果不願意使用layout,則可以設定layout為false,例如:

1 res.render("index",{"layout":false});

5、如果不想每個請求都單獨設定一次。可以使用全域設定:

12345 app.set("view options",{                                                                                                                                                                                                                                                                                    "layout":false                                                                                                                                                                                      });

6、ejs 裡,預設的閉合標記是 <%  .. %>,我們也可以定義自己的標籤。例如:

1234 app.set("view options",{                                                                                     "open":"{{",                                                                                     "close":"}}"});

nodejs express template (模版)的使用 (ejs + express)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.