从 Jade 模板访问 Express.js 请求或会话
我想知道是否有一种简单的方法可以从 Jade 模板中访问 Express.js 的 req 或会话变量,而无需通过正常响应传递它。
或者这是唯一的方法?
res.render('/', {
session: req.session
});
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.
Or is this the only way?
res.render('/', {
session: req.session
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
只需添加
Before
即可在 jade 中进行会话
Just add
Before
and get your session in jade
在express 3.x中,dynamicHelpers已被删除,因此您将需要使用中间件和res.locals的组合。假设我们想要在
/signup/new
视图中访问req.query
:现在任何使用
localQuery
中间件的路由都会有 <代码>res.locals.query 设置。然后可以在您的视图中将其作为查询
进行访问。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 accessreq.query
in a/signup/new
view:Now any route which uses the
localQuery
middleware, will haveres.locals.query
set. This can then be accessed in your view asquery
.您需要创建一个
dynamicHelper
供 Express 使用。然后在模板中,您可以使用
<%= session.logged_in %>
或其他内容。注意:DynamicHelpers 在 Express 3 中已弃用
You'll need to create a
dynamicHelper
for Express to use.Then inside your template, you can use
<%= session.logged_in %>
or whatever.Note: dynamicHelpers are deprecated in Express 3
只需使用中间件。
之后您可以使用“#{req}”在jade模板中引用它。
假设“req”中有一个“user”对象,并且“user”有一个方法“isAnonymous”,
如果你的 user.isAnonymous() 返回 true,
将呈现为:
just use a middleware.
After which you can use "#{req}" to refer to it in a jade template.
Suppose you have a 'user' object in 'req', and 'user' has a method 'isAnonymous',
if your user.isAnonymous() returns true,
will be rendered as :
有用
这对我在哈巴狗或翡翠视图用户中
this worked for me
in pug or jade view user
虽然 javascript 中总有一种方法可以逃脱作用域并向上爬行,但我真的真的真的真的真的强烈鼓励您寻找另一种方式。
考虑一下您的问题:我可以让我了解我的控制器的内部结构吗?
或者你真正想问的是:我可以让我的观点了解我的运行时的内部情况吗?
视图应该获取数据并将其转换为标记。就是这样。如果你做了其他任何事情,你就做错了。我不在乎它有多“容易”。这就是界面的意义所在。准确定义正在传递的内容,并且可以轻松地将一件事替换为另一件事。
While there is always a way in javascript to escape the scope and crawl upwards, I really really really really really strongly encourage you to find another way.
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.
您可以使用渲染函数来解决此问题,在该函数中,您可以使用渲染函数来渲染需要会话变量作为模板中可访问的局部变量的每个视图(例如,通常在用户登录时)。
这是一个函数的示例,但您可以根据需要进行调整:
然后可以像这样使用它:
并且在您的jade模板中,您可以像这样访问会话变量:
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:
Then it can be used like this:
and in your jade template you can access the session variables like this:
如果您的会话对象变量是全局声明的,那么
在全局
变量 sess
中在函数
sess=req.session
req.render('index.pug',{sess})
If your session object variable is declared globally then
In global
var sess
In function
sess=req.session
req.render('index.pug',{sess})