EJS渲染参数含义
EJS render parameters meaning
var express = require('express');
var app = express();
app.set('view engine','ejs');
var ejs = require('ejs'),
people = ['geddy', 'neil', 'alex'],
html = ejs.render('<%= people.join(", "); %>', {people: people});
app.get('/' , function(req, res){
res.render("basic",{html});
});
app.listen( 3000,function() {
console.log(html);
});
"basic" 是一个 basic.ejs 文件:
<html>
<title>Hello</title>
<body>
<%- html %>
</body>
</html>
这行得通,但我想知道这一行到底发生了什么
html = ejs.render('<%= people.join(", "); %>', {people: people});
声明{people: people}是做什么的?
谢谢
{people: people}
将人物数组放入对象中,因此 ['geddy', 'neil', 'alex']
与键 people
相关联。因此,当 EJS 呈现 <%= people.join(", "); %>
时,它会转到您传递给函数的对象 ({people: people}
),查找名为 people
的键并使用关联的值。
在你的情况下,你可以简单地替换这个:
html = ejs.render('<%= people.join(", "); %>', {people: people});
有了这个:
html = people.join(", ");
唯一不同的是这个
<%= Outputs the value into the template (HTML escaped)
因为你的数组是安全的,你不需要使用<%=
var express = require('express');
var app = express();
app.set('view engine','ejs');
var ejs = require('ejs'),
people = ['geddy', 'neil', 'alex'],
html = ejs.render('<%= people.join(", "); %>', {people: people});
app.get('/' , function(req, res){
res.render("basic",{html});
});
app.listen( 3000,function() {
console.log(html);
});
"basic" 是一个 basic.ejs 文件:
<html>
<title>Hello</title>
<body>
<%- html %>
</body>
</html>
这行得通,但我想知道这一行到底发生了什么
html = ejs.render('<%= people.join(", "); %>', {people: people});
声明{people: people}是做什么的?
谢谢
{people: people}
将人物数组放入对象中,因此 ['geddy', 'neil', 'alex']
与键 people
相关联。因此,当 EJS 呈现 <%= people.join(", "); %>
时,它会转到您传递给函数的对象 ({people: people}
),查找名为 people
的键并使用关联的值。
在你的情况下,你可以简单地替换这个:
html = ejs.render('<%= people.join(", "); %>', {people: people});
有了这个:
html = people.join(", ");
唯一不同的是这个
<%= Outputs the value into the template (HTML escaped)
因为你的数组是安全的,你不需要使用<%=