Cristian G
Cristian G

Reputation: 799

where I create or modify global variables of the view (ejs)

I need to modify the variable "<%= title %>", but I don't know where they are creating the variable or where I can change it.

I will like to know that other global variables can be found in the view, for example the variable <%= session %> I did not send in view, but for some reason already there. in "express" it had touched me do something like the following:

"res.render('session/new', {title: "Log in", session: req.session});"

so I'd like to know where they are creating the "session" variable, because I do not like to go out there not knowing some things XD

thank you very much in advance for the help.

Upvotes: 1

Views: 3955

Answers (1)

aludvigsen
aludvigsen

Reputation: 5981

I will try to answer some of your questions regarding local/global variables in Sails.js.

Local variables

Sails.js is built on top of ExpressJS and uses much of the same techniques for sending data to the view. For example, to send local variables to the view in context:

In your controller:

newSession: function(req, res, next) {
    res.view('session/new', {
        title: "Log in", 
        session: req.session
    });
});

Global variables

Sails.js have already some global variables. title maybe one of them. For example sails is a global variable. To that variable you can attach your own custom variables. To do that:

  • Create a new file in config folder.
  • Name it whatever you like, and use the module.exports convention.

In your config/myGlobals.js file:

module.exports.myGlobalVariables = {
    globalOne: "This is a string",
    globalTwo: function(){ return "myGlobal"; }
}

Now you can access these variables in all your controllers/views using:

sails.config.myGlobalVariables.globalOne; //returns: This is a string
sails.config.myGlobalVariables.globalTwo; //returns: myGlobal

Upvotes: 5

Related Questions