[英]What's the difference between res.render() and ejs.render() in Node.js and Express app
I use EJS template engine in my Node.js and Express app, and have used its functionality and rendering so far, and haven't had any problems so far. 我在我的Node.js和Express应用程序中使用EJS模板引擎,到目前为止已经使用了它的功能和渲染,到目前为止还没有任何问题。
However, while I always used the syntax res.render(filename, options, callback)
in my server-side program to render the contents of the file, I wonder what's the difference between res.render()
and ejs.render()
. 然而,虽然我总是在服务器端程序中使用语法
res.render(filename, options, callback)
来呈现文件的内容,但我想知道res.render()
和ejs.render()
之间的区别是什么。
It looks like both methods take a rendering filename as a first argument and Object to embed to the file as a second argument (like {title: "title here"}
). 看起来两种方法都将渲染文件名作为第一个参数,将Object作为第二个参数嵌入到文件中(如
{title: "title here"}
)。 res.render()
can take a callback function as a third (optional) argument and I've used it whenever I want to make use of a nested rendering, but from the documentation of the EJS Github repository, it might not be able to take the callback function, again at least the documentation at the Github repository doesn't take the argument (though its argument would be optional anyway). res.render()
可以将回调函数作为第三个(可选)参数使用,每当我想使用嵌套渲染时我都会使用它,但是从EJS Github存储库的文档中,它可能无法使用采用回调函数,至少Github存储库中的文档不接受参数(尽管它的参数无论如何都是可选的)。
So I wonder, what's the difference between res.render()
and ejs.render()
. 所以我想知道,
res.render()
和ejs.render()
之间的区别是什么。 If only res.render()
can take the third argument, what's the point of using ejs.render()
? 如果只有
res.render()
可以接受第三个参数,那么使用ejs.render()
什么意义呢? Or is there anything that ejs.render()
can use that res.render()
cannot? 或者是否有任何
ejs.render()
可以使用res.render()
不能? And in general which function should I use in my app? 一般来说,我应该在我的应用程序中使用哪个功能?
I write the app.set('view engine', 'ejs');
我写了
app.set('view engine', 'ejs');
to use EJS in my app for your information. 在我的应用程序中使用EJS来获取您的信息。
Use res.render()
. 使用
res.render()
。
If you're already using Express to render views you shouldn't need to use EJS directly. 如果您已经使用Express来渲染视图,则不需要直接使用EJS。 Just make sure you have it listed as a dependency in your
package.json
and Express will take care of the rest! 只需确保将它列为
package.json
的依赖项,Express将负责其余部分!
Here's some more details: 这里有一些更多的细节:
Calling ejs.render()
or ejs.renderFile()
bypasses the Express view engine. 调用
ejs.render()
或ejs.renderFile()
绕过Express视图引擎。 Really all that means is you have to provide absolute paths to EJS and you have to send the rendered HTML to the client. 实际上,这意味着您必须提供EJS的绝对路径,并且必须将呈现的HTML发送到客户端。
This: 这个:
app.get('/', function (req, res) {
res.render('index.ejs');
});
Is equivalent to this: 相当于:
app.get('/', function (req, res) {
res.send(ejs.renderFile(__dirname + '/views/index.ejs'));
});
The callback parameter in res.render()
is there to support view engines that need to return asynchronously. res.render()
的回调参数用于支持需要异步返回的视图引擎。 EJS uses fs.readFileSync
to render its templates so ejs.render()
and ejs.renderFile()
don't require a callback—they just return the rendered HTML. EJS使用
fs.readFileSync
来呈现其模板,因此ejs.render()
和ejs.renderFile()
不需要回调 - 它们只返回呈现的HTML。
The one scenario I can think of where you might use EJS directly is if you want to “compile” a template into a function that you can call later: 我可以想到你可以直接使用EJS的一个场景是,如果你想将模板“编译”成一个稍后可以调用的函数:
var ejs = require('ejs'),
read = require('fs').readFileSync;
var template = ejs.compile(read('path/to/template.ejs', 'utf-8'));
console.log(template());
This was a bit of info for me as i'm not familiar with ejs
. 这对我来说有点信息,因为我不熟悉
ejs
。 But I have encountered a scenario where I don't have res
variable. 但是我遇到了一个我没有
res
变量的场景。 For instance I need to send a email with a html template. 例如,我需要发送一封带有html模板的电子邮件。 So in this use case, I found that we can only
ejs.render()
to use the template to email. 所以在这个用例中,我发现只有
ejs.render()
才能使用模板发送电子邮件。 Hence, ejs.render()
is important as res.render()
. 因此,
ejs.render()
作为res.render()
非常重要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.