本文介绍了快速res.locals.some变量在hbs(手柄模板)中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的会话变量传递给我的句柄模板,但是我被卡住了。现在我在我的app.configure函数中使用这个:

I am trying to pass my session variables to my handlebars templates but am getting stuck. Right now I am using this in my app.configure function:

app.use(function(req, res, next){
        res.locals.session = req.session;
        console.log(res.locals.session);
        next();
});

它正确记录到控制台,但是当我尝试在我的把手中使用会话变量模板,没有显示。这是我的模板的一部分:

It logs correctly to the console, but when I try to use the "session" variable in my handlebars template, nothing shows up. Here is part of my template:

<body>
        <nav>
            {{> topBarPartial}}

            {{> secondaryBarPartial}}
        </nav>
        <div>
            <p>before</p>
            {{session}}
            <p>after</p>
            {{> mainPartial}}
        </div>

        {{> footerPartial}}
</body>

这是控制台正在记录的内容:

Here is what is being logged by the console:

{ cookie: 
   { path: '/',
     _expires: null,
     originalMaxAge: null,
     httpOnly: true },
  userId: 45253262,
  name: 'Austin' }

任何想法?

推荐答案

我终于找到了我的解决方案。事实证明,我这样说:

I finally found my solution. It turns out that I was calling this:

app.use(function(req, res, next){
        res.locals.session = req.session;
        console.log(res.locals.session);
        next();
});

after

app.use(app.router);

它实际上需要在app.router之前,但在

It actually needs to be before the app.router, but after

app.use(express.session({
        secret: '***********'
    }));

这篇关于快速res.locals.some变量在hbs(手柄模板)中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 00:08