0

I would like to set a global variable in EJS to show/hide some elements. I would like to be able to set this variable once and have it propogate through all the views. What is the best way to do this?

The other parts of my stack are node.js/express/ejs.

Added code for Vadim:

@index = (req, res) ->
    res.render 'index',
    view: 'index'
    gloabals: app.locals    // is there a way to have this line for all controllers?

1 Answer 1

1

You can use express app.locals.

var app = express();
app.locals.someFlag = true;
Sign up to request clarification or add additional context in comments.

3 Comments

is there a way to add this to all the controllers so all the ejs templates will get the variables. I edited the question above to give some more context.
app.locals is global for application. This data should be provided to all your templates. You don't need set in in some controller. It need to be set globally on app level.
one more thing... Do you know how I can pass in an app.locals variable to a connect-assets compiled file. Eg. get a variable into js('file.coffee')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.