在expressjs渲染中消除布局局部变量

发布于 2024-10-14 20:55:44 字数 477 浏览 5 评论 0原文

我在nodejs中使用express,并试图保持我的视图渲染干燥。在我的布局中,我有类似以下内容的内容(我正在使用玉)

body
  nav.login
    -if(currentUser)
      ="logged in information"
    -else
      ="logged out information"
  !=body

问题是每次渲染时,我现在都需要执行

res.render('anything.jade',{
  locals: {
    currentUser: req.session.currentUser,
    /*all of my other locals*/
  }
});

所有渲染调用并添加似乎很痛苦然后,如果我必须向布局添加任何其他本地变量,则执行相同的操作。有没有什么方法可以避免在我渲染的每个地方重新输入“currentUser”

I am using express in nodejs, and am trying to keep my view rendering dry. Inside my layout, I have something like the following (I'm using jade)

body
  nav.login
    -if(currentUser)
      ="logged in information"
    -else
      ="logged out information"
  !=body

The problem is that every time I render, I'm now required to have

res.render('anything.jade',{
  locals: {
    currentUser: req.session.currentUser,
    /*all of my other locals*/
  }
});

It seems like a pain to have to go through all of my rendering calls and add that, and then do the same thing if I have to add any other locals to the layout. Is there some way to keep from having to retype 'currentUser' into the locals everywhere I render

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

忆梦 2024-10-21 20:55:44

我知道这个答案已经晚了,但对于后代来说:如果您使用的是express,则应该使用提供的动态助手,它们是可查看的辅助函数,以 req,res 作为参数。在您的情况下:

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

这意味着您的视图现在可以调用 #{user}

I know this answer is late, but for posterity: if you're using express, you should use the supplied dynamic helpers, which are view-accessible helper functions which take req,res as arguments. In your case:

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

Which means your view could now call #{user}

望她远 2024-10-21 20:55:44

我最终做了以下事情:

首先,我设置了一些局部变量

app.use(function(req,res,next){
  req.localvars = new Object;
  next();
});

,稍后,我设置了一些局部变量,

app.use(function(req,res,next){
  req.localvars.currentUser = req.session.currentUser;
  next();
});

我设置了渲染函数,

function renderer(file){  
  return function(req,res,next){
    res.render(file,{locals:req.localvars});
  }
}

然后为路由函数

app.get('/',blah,blah,whatever,setSomeMoreLocalsHere,renderer(index.jade));

设置了可能有更好的东西,但这是我找到的最佳解决方案,所以远的

I ended up doing the following:

first, I set up some local variables

app.use(function(req,res,next){
  req.localvars = new Object;
  next();
});

later, I set some local variables

app.use(function(req,res,next){
  req.localvars.currentUser = req.session.currentUser;
  next();
});

I set up a render function

function renderer(file){  
  return function(req,res,next){
    res.render(file,{locals:req.localvars});
  }
}

then for the routing function

app.get('/',blah,blah,whatever,setSomeMoreLocalsHere,renderer(index.jade));

There might be something better, but it's the best solution I've figured out so far

云醉月微眠 2024-10-21 20:55:44

最简单的解决方案是使用返回局部对象的函数。

function(req) {
    return {
        currentUser: req.session.currentUser,
        /*all of my other locals*/
    };
}

res.render('anything.jade',{
  locals: defaultLocals(req)
});

The easiest solution would be to use a function that returns a locals object.

function(req) {
    return {
        currentUser: req.session.currentUser,
        /*all of my other locals*/
    };
}

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