简体   繁体   English

如何在Jade中分配变量?

[英]How can assign a variable in Jade?

what my app does is, once a user is signed in, the user information gets exported so that the router use it to update the profile template. 我的应用程序要做的是,一旦用户登录,便会导出用户信息,以便路由器使用它来更新配置文件模板。 But then what I want my app to do is, while the user is still logged in, if he/she goes to the home page, I don't want the navigation bar to be rendered with the signin and signup links rather want is to render the navigation bar with signout and username link which is the same one as the profile page. 但是,然后我要我的应用程序执行的是,当用户仍然登录时,如果他/她转到主页,我不希望导航栏显示为具有登录和注册链接,而是希望使用退出和用户名链接(与个人资料页面相同)呈现导航栏。

So my error is when the user requests for the home page the router passes a variable named isSignedIn along with the template name. 所以我的错误是,当用户请求主页时,路由器将名为isSignedIn的变量与模板名称一起传递。 And I want the value of the variable(isSignedIn) to be stored in another variable in the jade file so that if the user isSignedIn, jade it will render different navigation bar for homepage. 我希望将变量(isSignedIn)的值存储在jade文件中的另一个变量中,这样,如果用户使用isSignedIn,则jade将为主页呈现不同的导航栏。

jade could not execute this : -var isSignedin = #{isSignedIn} 玉无法执行此操作:-var isSignedin =#{isSignedIn}

Some of my code looks like this : 我的一些代码如下所示:

//app.js //app.js

app.post('/signup',
    passport.authenticate('signup_local_strategy',
        {successRedirect:'/profile',
         failureRedirect:'/signup_error'
        }));
passport.use('signup_local_strategy',new localStrategy(
    {passReqToCallback: true},
    function(req, username, password, done)
    {
        user_model.findOne({username:username},function(err, user){
            if(err)
            {
                return done(err);
            }
            if(user == null)
            {
                var new_user = new user_model({
                    username: username,
                    password: password,
                    university: req.body.university,
                    hometown: req.body.hometown
                });
                new_user.save(function(err){
                    if(err)
                    {
                        throw err;
                    }
                });
                user = new_user;    //Assigned the variable new_user to user to authematically serialize the new user.          
                return(done(null, user));
            }
            if(user.username == username)
                {
                    return(done(null, false));
                }       
        });     
    }
));
app.get('/profile', routes.userProfileResponseHandler);
app.get('/', routes.indexResponseHandler); 

//serialize user and export the the user information so that the router update the view
passport.serializeUser(function(user, done){
    done(null, user.id);
    exports.isSignedIn = true;
    exports._id = user.id;
    exports.username = user.username;
    exports.university = user.university;
    exports.hometown = user.hometown;
});

routes.js routes.js

exports.indexResponseHandler = function (req, res){
    res.render('index', {title: "College Clubs MN", isSignedIn: app.isSignedIn});
}

//index.jade //index.jade

- var iSignedin = #{isSignedIn} // It could not assign #{isSignedIn} in the variable.
ul(class="nav navbar-nav navbar-right")
    if(isSignedIn=='true')
        li
            a(href="/signout") Signout
        li
            a(href="/profile") #{username}
    else
        li
            a(href="/signin") Sign in
        li
            a(href="/signup") Sign up

Thank you in advance :)! 先感谢您 :)!

The properties of the Object given to res.render() will already be treated as locals , or local variables, within the template. 赋予res.render()Object的属性在模板中已被视为locals或局部变量。

// - var iSignedin = #{isSignedIn} // not necessary

ul(class="nav navbar-nav navbar-right")
    if isSignedIn
        // ...

There isn't an additional layer of separation between res.render() and index.jade that requires the variable to be declared and value to be output to it. res.render()index.jade之间没有额外的隔离层,它需要声明变量并将值输出到该变量。


The error is because Jade's interpolation syntax, #{...} , isn't valid within code . 该错误是因为Jade的插值语法#{...}code中无效。 It isn't intended to be used with every variable reference, just when needing to output the result of code within plain text . 它不打算与每个变量引用一起使用,仅当需要在纯文本中输出代码结果时才使用。

While redundant, the line would be syntactically valid as: 虽然多余,但该行在语法上有效为:

- var isSignedIn = isSignedIn;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM