从 Jade 模板访问 Express.js 请求或会话

发布于 2024-11-15 05:21:26 字数 164 浏览 3 评论 0原文

我想知道是否有一种简单的方法可以从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

≈。彩虹 2024-11-22 05:21:26

只需添加

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);

即可在 jade 中进行会话

p #{session}

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}
半山落雨半山空 2024-11-22 05:21:26

在express 3.x中,dynamicHelpers已被删除,因此您将需要使用中间件和res.locals的组合。假设我们想要在 /signup/new 视图中访问 req.query

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);

现在任何使用 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 access req.query in a /signup/new view:

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 localQuery middleware, will have res.locals.query set. This can then be accessed in your view as query.

撕心裂肺的伤痛 2024-11-22 05:21:26

您需要创建一个 dynamicHelper 供 Express 使用。

app.dynamicHelpers({
    session: function (req, res) {
        return req.session;
    }
});

然后在模板中,您可以使用 <%= session.logged_in %> 或其他内容。

注意:DynamicHelpers 在 Express 3 中已弃用

You'll need to create a dynamicHelper for Express to use.

app.dynamicHelpers({
    session: function (req, res) {
        return req.session;
    }
});

Then inside your template, you can use <%= session.logged_in %> or whatever.

Note: dynamicHelpers are deprecated in Express 3

养猫人 2024-11-22 05:21:26

只需使用中间件。

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();
});

之后您可以使用“#{req}”在jade模板中引用它。

假设“req”中有一个“user”对象,并且“user”有一个方法“isAnonymous”,
如果你的 user.isAnonymous() 返回 true,

p #{req.user.isAnonymous()}

将呈现为:

<p>true</p>

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.

Suppose you have a 'user' object in 'req', and 'user' has a method 'isAnonymous',
if your user.isAnonymous() returns true,

p #{req.user.isAnonymous()}

will be rendered as :

<p>true</p>
说不完的你爱 2024-11-22 05:21:26

有用

app.use(function(req,res,next){
   res.locals.user = req.user;
   next();
});

这对我在哈巴狗或翡翠视图用户中

#{user.email}

this worked for me

app.use(function(req,res,next){
   res.locals.user = req.user;
   next();
});

in pug or jade view user

#{user.email}
最佳男配角 2024-11-22 05:21:26

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

相思故 2024-11-22 05:21:26

您可以使用渲染函数来解决此问题,在该函数中,您可以使用渲染函数来渲染需要会话变量作为模板中可访问的局部变量的每个视图(例如,通常在用户登录时)。

这是一个函数的示例,但您可以根据需要进行调整:

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

然后可以像这样使用它:

app.get("/url", function(req, res){

    req.session.user_email = user_email;

    renderView(res, "template_name", { local_variables: local_variables }, req.session)
});

并且在您的jade模板中,您可以像这样访问会话变量:

div.user-email #{session.user_email}

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:

div.user-email #{session.user_email}
各自安好 2024-11-22 05:21:26

如果您的会话对象变量是全局声明的,那么
在全局

变量 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})

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文