最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node.js - How can I use my local variable with EJS view - Stack Overflow

programmeradmin3浏览0评论

I try to used my local variable on EJS. It's just a varaiable to check if the user is connected. -> Like the last post on this link: How to know if user is logged in with passport.js?

So,

app.js

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

view EJS

<% if (login) { %>
      <div id="subMenuUser">
        <div class="btn-group">
          <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
            </span>Menu<span class="caret"></span>
          </button>
          <ul class="dropdown-menu pull-right" role="menu">
            <li><a href="#" role="menuitem">Menu Item 1</a></li>
            <li><a href="#" role="menuitem">Menu Item 2</a></li>
            <li><a href="#" role="menuitem">Menu Item 3</a></li>
          </ul>
        </div>
      </div>
 <% } %>

I get an error:

login is not defined

I would like to use this variable with all view.. possible ?

Before this method, I sent each time my variable in render like that:

res.render('ad/index', { login: req.session.user });

I try to used my local variable on EJS. It's just a varaiable to check if the user is connected. -> Like the last post on this link: How to know if user is logged in with passport.js?

So,

app.js

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

view EJS

<% if (login) { %>
      <div id="subMenuUser">
        <div class="btn-group">
          <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
            </span>Menu<span class="caret"></span>
          </button>
          <ul class="dropdown-menu pull-right" role="menu">
            <li><a href="#" role="menuitem">Menu Item 1</a></li>
            <li><a href="#" role="menuitem">Menu Item 2</a></li>
            <li><a href="#" role="menuitem">Menu Item 3</a></li>
          </ul>
        </div>
      </div>
 <% } %>

I get an error:

login is not defined

I would like to use this variable with all view.. possible ?

Before this method, I sent each time my variable in render like that:

res.render('ad/index', { login: req.session.user });
Share Improve this question edited May 23, 2017 at 12:16 CommunityBot 11 silver badge asked May 29, 2014 at 21:25 JohnnyJohnny 3,3184 gold badges18 silver badges28 bronze badges 1
  • I haven't used EJS but maybe something like login = typeof(req.session.use) != 'undefined' ? req.session.use : { } will do the trick for you. – Jonast92 Commented May 29, 2014 at 22:21
Add a ment  | 

2 Answers 2

Reset to default 8

or if you want to do without creating Middleware, you can also directly use following check in your ejs file:

if(locals.login) { .... }

Make sure that you are declaring that middleware function that sets res.locals.login before you declare your app.router. This ensures that the route rendering your template does not render the template before your middleware can set res.locals.login. Something along the lines of:

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

app.configure(function(){
    ...
    app.use(app.router);
    ...
});

It appears others have asked a similar question, but with other templating engines: Accessing res.locals from Backbone.Router templates

发布评论

评论列表(0)

  1. 暂无评论