node.js 从 Jade 模板访问 Express.js 请求或会话
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6331776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accessing Express.js req or session from Jade template
提问by MrBojangles
I am wondering if there is an easy way to access Express.js' req or session variables from within a Jade template without passing it in through the normal response.
我想知道是否有一种简单的方法可以从 Jade 模板中访问 Express.js 的 req 或会话变量,而无需通过正常响应传递它。
Or is this the only way?
或者这是唯一的方法?
res.render('/', {
session: req.session
});
采纳答案by Dominic Barnes
You'll need to create a dynamicHelperfor Express to use.
您需要创建一个dynamicHelper供 Express 使用。
app.dynamicHelpers({
session: function (req, res) {
return req.session;
}
});
Then inside your template, you can use <%= session.logged_in %>or whatever.
然后在您的模板中,您可以使用<%= session.logged_in %>或其他任何内容。
Note: dynamicHelpers are deprecated in Express 3
注意:在 Express 3 中不推荐使用 dynamicHelper
回答by Ajouve
Just add
只需添加
app.use(express.cookieParser());
app.use(express.session({secret: '1234567890QWERTY'}));
app.use(function(req,res,next){
res.locals.session = req.session;
next();
});
Before
前
app.use(app.router);
and get your session in jade
并在玉中获得您的会话
p #{session}
回答by David Weldon
In express 3.x, dynamicHelpers have been removed so you will need to use a combination of middleware and res.locals. Let's say we want to access req.queryin a /signup/newview:
在 express 3.x 中,dynamicHelpers 已被删除,因此您需要使用中间件和res.locals. 假设我们想req.query在/signup/new视图中访问:
localQuery = function(req, res, next) {
res.locals.query = req.query;
next();
};
newSignup = function(req, res) {
res.render('signup/new');
};
app.get('signup/new', localQuery, newSignup);
Now any route which uses the localQuerymiddleware, will have res.locals.queryset. This can then be accessed in your view as query.
现在任何使用localQuery中间件的路由都将res.locals.query设置。然后可以在您的视图中将其访问为query。
回答by kaisa1028
just use a middleware.
只需使用中间件。
app.use(function (req, res, next) {
var origRender = res.render;
res.render = function (view, locals, callback) {
if ('function' == typeof locals) {
callback = locals;
locals = undefined;
}
if (!locals) {
locals = {};
}
locals.req = req;
origRender.call(res, view, locals, callback);
};
next();
});
After which you can use "#{req}" to refer to it in a jade template.
之后,您可以使用“#{req}”在玉石模板中引用它。
Suppose you have a 'user' object in 'req', and 'user' has a method 'isAnonymous', if your user.isAnonymous() returns true,
假设您在“req”中有一个“user”对象,并且“user”有一个方法“isAnonymous”,如果您的 user.isAnonymous() 返回 true,
p #{req.user.isAnonymous()}
will be rendered as :
将呈现为:
<p>true</p>
回答by Sultan Zaid
this worked for me
这对我有用
app.use(function(req,res,next){
res.locals.user = req.user;
next();
});
in pug or jade view user
在 pug 或 jade 视图用户中
#{user.email}
回答by st0ne
You could solve this with a render function in which you use to render every view that needs the session variable as a local variable accessible in the template(usually when a user is logged in for example).
您可以使用渲染函数解决此问题,在该函数中,您可以将需要会话变量的每个视图渲染为可在模板中访问的局部变量(例如,通常在用户登录时)。
Here is an example of a function but you can adjust as you like:
这是一个函数示例,但您可以根据需要进行调整:
var renderView = function(res, template, context, session, cb) {
context.session = session;
if(cb){
res.render(template, context, function(error, html){
cb(error, html)
}
} else {
res.render(template, context)
}
}
Then it can be used like this:
然后它可以像这样使用:
app.get("/url", function(req, res){
req.session.user_email = user_email;
renderView(res, "template_name", { local_variables: local_variables }, req.session)
});
and in your jade template you can access the session variables like this:
在你的 jade 模板中,你可以像这样访问会话变量:
div.user-email #{session.user_email}
回答by jcolebrand
While there is always a way in javascript to escape the scope and crawl upwards, I reallyreally reallyreally reallystrongly encourage you to find another way.
虽然在 javascript 中总有一种方法可以逃避范围并向上爬行,但我真的真的真的真的真的非常强烈地鼓励您找到另一种方法。
Consider what you're asking: Can I have my view know about the guts of my controller?
考虑一下您要问的问题:我可以让我的视图了解控制器的内部结构吗?
Or what you're really asking: Can I have my view know about the guts of my runtime?
或者您真正要问的是:我可以让我的视图了解我的运行时的内容吗?
A view is supposed to take data and transform it into markup. That's IT. If you do anything else, you're doing it wrong. I don't care how "easy" it is. That's the point of an interface. To define exactly what is being passed, and to make it easy to replace one thing with another thing.
视图应该获取数据并将其转换为标记。就是这样。如果你做任何其他事情,你就做错了。我不在乎它有多“容易”。这就是接口的意义所在。准确定义正在传递的内容,并轻松地将一件事替换为另一件事。

