首页 > 代码库 > nodejs express template (模版)的使用 (ejs + express)

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":"}}"
});

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