简体   繁体   中英

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. 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.

jade could not execute this : -var isSignedin = #{isSignedIn}

Some of my code looks like this :

//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

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

//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.

// - 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.


The error is because Jade's interpolation syntax, #{...} , isn't valid within 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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