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

1
2
3
4
5
6
7
8
9
var app=require( "express" ).createServer();                                                                                                                           
                                                                                                                                                                                                                                                          
app.set( "view engine" , "ejs" );                                                                                                                           
                                                                                                                                                                                                                                                          
app.get( "/" , function (req,res){                                                                                                                           
     res.render( "index" ,{ "title" : "test" });                                                                                                                           
});                                                                                                                           
                                                                                                                                                                                                                                                          
app.listen(3000);

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

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

ejs可經過 npm install ejs 來進行安裝express

二、在app.get中。npm

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

  1. 會讀取 ./views/index.ejs文件的內容,而後將其中的title變量替換爲test,例如<%=title%>會變爲test。假設返回的內容爲content1ui

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

三、在2中,若是不肯意使用默認的layout.ejs,可自行指定。例如:code

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

ci

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

四、若是不肯意使用layout,則能夠設置layout爲false,例如:get

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

五、若是不想每一個請求都單獨設置一次。能夠使用全局設置:

1
2
3
4
5
app.set( "view options" ,{                                                                                         
                                                                                                                                                                                      
     "layout" : false
                                                                                                                                                                                      
});

六、ejs 裏,默認的閉合標記是 <%  .. %>,咱們也能夠定義本身的標籤。例如:

1
2
3
4
app.set( "view options" ,{                                                                                 
    "open" : "{{" ,                                                                                 
    "close" : "}}"
});
相關文章
相關標籤/搜索