Thursday, 25 May 2017

Making Variable Passed Through Pug Available to Javascript

I have a Pug view that gives a set of links to a user. Before you get to the page the user has already authenticated and I have the username and their department in session variables. I can pass them as variables to the view like this:

res.render('landingpage', { title: 'Landing Page', 
                            username:   req.session.username,
                            department: req.session.department  });

And then in the view I have this line and it works:

p(class='navbar-text') Welcome #{username} from #{department}

which prints "Welcome Bob from Accounting" at the top with no problem.

But what I need to do is control whether some of the links are visible based upon the passed in department. (The department was discovered in the authentication function that passed the user onto the landing page and placed into the session.)

I was trying to place this into the document ready function but that doesn't work as it is undefined. What I need to do is to be able to change the visibility attribute and the onclick event for a link based upon the department. I have a JSON configuration file that tells me the departments allowed to access the link but I can't figure out how to translate that department variable into a javascript function that I can call to change the visibility.

I've tried to add it to a document ready function as department and #{department} but it just ends up either not knowing what it is or using it like the literal string. Any ideas of how to proceed?



via spfurlong

No comments:

Post a Comment