0

i want to be able to pass a global object that i will later on be use for whatever to a jade template

lets say:

app.get("/", function(req, res){

    var options = {
        chGlobal : {// this is the object i want to be a global
            "property1" : 1,
            "property2" : 2,
            "property3" : 3,
        }
    };

    jade.renderFile(__dirname +'/tpl/main.jade', options, function (err, html) {
        console.log(err, html);
        if (err)
        {
            throw err
        }
        else
        {
            res.send(html);
        }
    });
});

i would like to be able to use "chGlobal " in other scripts that are loaded. as if chGlobal was defined in the global scope.

Thanks

Neta Meta
  • 4,001
  • 9
  • 42
  • 67

1 Answers1

4

If you use jade as a view engine through express, like this:

app.set('views', __dirname); // this will be where your views are located.
app.set('view engine', 'jade');

You can specify local variables using res.locals.variable.

Example)

app.get("/", function(req, res){

    res.locals.options = {
        chGlobal : {// this is the object i want to be a global
            "property1" : 1,
            "property2" : 2,
            "property3" : 3,
        }
    };

    res.render('main');
});

Then in Jade, you can access options variable.

You can write a middleware to automagically append the global variables, like this:

app.get("/", registerGlobals, function(req, res) {

Then the middleware function would be:

function registerGlobals(req, res, next) {
    res.locals.options = {
        chGlobal : {// this is the object i want to be a global
            "property1" : 1,
            "property2" : 2,
            "property3" : 3,
        }
    };

    next();
}

More tutorials on how to use jade here: http://runnable.com/UTlPPF-f2W1TAAEe/render-jade-with-express-for-node-js

tpae
  • 6,286
  • 2
  • 37
  • 64
  • yea i do use it like that, but i dont need local i need global variables. lets say i have a template - in that template i load another JS header that contains all the functionality of that page i would like to be able to use variables i "loaded" from the server in that file. – Neta Meta Jun 20 '14 at 00:07
  • I am a bit confused, if you use include statements in jade (referring to another template), you can access the variables. Global variables, you want them to be accessible throughout your jade templates, right? – tpae Jun 20 '14 at 00:09
  • not using include using `script(src="/js/utilFunctions.js" type="text/javascript")` or `link(href="/css/style.css" rel="stylesheet" type="text/css")` for css, i want to be able use the variable inside utilFunction.js – Neta Meta Jun 20 '14 at 00:10
  • If you want to expose the variables on a client side script, you have to do it manually (explicitly stating var options = '#{options}';) or make an endpoint called /js/utilFunctions.js which is an explicit express endpoint to render your JS files. – tpae Jun 20 '14 at 00:15
  • Even if you are writing javascript, there's still a difference between server side and client side. Unfortunately, you have to expose variables manually. – tpae Jun 20 '14 at 00:15
  • so this `options = '#{options}';` with in a jade template will create option global ? is that how you use "local" variables in js within a jade template ? you add '#{}' to the name of the variable ? if so this is exactly what i am looking for i will try that now – Neta Meta Jun 20 '14 at 00:20
  • I've tried the following: `script(type='text/javascript'). var chGlobal = '#{options}'; var chGlobala = '#{chGlobal}'; console.log('#{chGlobal}', '#{options}');` did not work. – Neta Meta Jun 20 '14 at 00:27
  • so after some changes i managed to make it work sort of. when i console.log i get "[object Object]" now. – Neta Meta Jun 20 '14 at 01:06
  • Oh my bad, you need to call JSON.stringify, so #{JSON.stringify(chGlobal)}, make sure you set res.locals.JSON = JSON; – tpae Jun 20 '14 at 01:08
  • Check this link: http://stackoverflow.com/questions/8437295/jade-template-how-to-pass-concrete-object-to-pages – tpae Jun 20 '14 at 01:08
  • If you do stringify, you won't need to put it around quotes anymore. '' – tpae Jun 20 '14 at 01:13
  • what do you mean by make sure you set res.locals.JSON = JSON ? – Neta Meta Jun 20 '14 at 01:18
  • well i did `script(type='text/javascript'). var options = JSON.parse(!{JSON.stringify(options)});` without adding json.parse it wont work. it will give me a string which i dont need. i was thinking maybe i would stringify in the server and parse in the client might also work – Neta Meta Jun 20 '14 at 01:25
  • That will work, get rid of JSON.parse, just stringify should be fine – tpae Jun 20 '14 at 01:27
  • 1
    yea without parse worked fine ones i placed it in the correct place :-) thanks for sticking with me. – Neta Meta Jun 20 '14 at 01:29