在Node.js和Express应用程序中res.render()和ejs.render()之间有什么区别? [英] What's the difference between res.render() and ejs.render() in Node.js and Express app

查看:375
本文介绍了在Node.js和Express应用程序中res.render()和ejs.render()之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Node.js和Express应用程序中使用 EJS 模板引擎,并使用其功能和渲染到目前为止,至今没有遇到任何问题。



然而,虽然我一直使用语法 res.render(filename,options ,回调)在我的服务器端程序中渲染文件的内容,我不知道 res.render() ejs.render()



看起来两个方法都将一个渲染文件名作为第一个参数,并将Object嵌入作为第二个参数(如 {title:title here} )。 res.render()可以将回调函数作为第三个(可选)参数,并且每当我想使用嵌套呈现,但是从EJB Github存储库的文档,它可能无法使用回调函数,至少再次,Github存储库中的文档不会使用参数(尽管它的参数是可选的)。



所以我想知道,$ code> res.render()和 ejs.render()。如果只有 res.render()可以使用第三个参数,使用 ejs.render()有什么意义?或者有什么可以使用 ejs.render()可以使用 res.render()不能吗?一般来说,我应该在我的应用程序中使用哪个功能?



我写了 app.set('view engine','ejs'); 在我的应用程序中使用EJS以获取您的信息。

解决方案

使用 res如果您已经在使用Express来呈现视图,则不需要直接使用EJS。只需确保您将其列为您的 package.json 中的依赖关系,Express将会照顾其余!



这里有一些更多的细节:



调用 ejs.render() ejs。 renderFile()绕过Express视图引擎。所有这一切都意味着你必须提供EJS的绝对路径,你必须将呈现的HTML发送给客户端。



这个:

  app.get('/',function(req,res){
res.render('index.ejs');
});

相当于此:

  app.get('/',function(req,res){
res.send(ejs.renderFile(__ dirname +'/views/index.ejs'));
});

res.render()是支持需要异步返回的视图引擎。 EJS使用 fs.readFileSync 来呈现其模板,以便 ejs.render() ejs。 renderFile()不需要回调 - 他们只是返回渲染的HTML。



我可以想到一个方案可以使用EJS直接就是要将模板编译成一个可以稍后调用的函数:

  var ejs = require 'ejs'),
read = require('fs')。readFileSync;

var template = ejs.compile(read('path / to / template.ejs','utf-8'));

console.log(template());


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.

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().

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"}). 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).

So I wonder, what's the difference between res.render() and ejs.render(). If only res.render() can take the third argument, what's the point of using ejs.render()? Or is there anything that ejs.render() can use that res.render() cannot? And in general which function should I use in my app?

I write the app.set('view engine', 'ejs'); to use EJS in my app for your information.

解决方案

Use res.render().

If you’re already using Express to render views you shouldn’t need to use EJS directly. Just make sure you have it listed as a dependency in your package.json and Express will take care of the rest!

Here’s some more details:

Calling ejs.render() or ejs.renderFile() bypasses the Express view engine. Really all that means is you have to provide absolute paths to EJS and you have to send the rendered HTML to the client.

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. 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.

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:

var ejs = require('ejs'),
    read = require('fs').readFileSync;

var template = ejs.compile(read('path/to/template.ejs', 'utf-8'));

console.log(template());

这篇关于在Node.js和Express应用程序中res.render()和ejs.render()之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆