Accessing Express.js req or session from Jade template

后端 未结 7 1243
南方客
南方客 2020-12-13 18:49

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.

相关标签:
7条回答
  • 2020-12-13 19:29

    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.

    0 讨论(0)
  • 2020-12-13 19:31

    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

    0 讨论(0)
  • 2020-12-13 19:35

    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}
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-13 19:40

    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>
    
    0 讨论(0)
  • 2020-12-13 19:45

    this worked for me

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

    in pug or jade view user

    #{user.email}
    
    0 讨论(0)
提交回复
热议问题