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

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、如果不想每个请求都单独设置一次。可以使用全局设置:

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

6、ejs 里,默认的闭合标记是 <%  .. %>,我们也可以定义自己的标签。例如:

1
2
3
4
app.set("view options",{                                                                                 
   "open":"{{",                                                                                 
   "close":"}}"
});

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。