What is the best way to pass global config variables to handlebars template?
express, handlebars.js, javascript, node.js
Solution
You would set the app locals:
var app = express()
app.locals = {
config: config,
templateVar: 'test'
}
Edit:
Your routes will look something like this:
app.get('/', function(req, res) {
res.render('index', {config: config});
})
What this does, is then update the app.locals variable in express to look like this:
app.locals = {
config: config
}
All the app.local variables are then accessible in your templates via:
{{config}}
//which is really
app.locals['config']
So, in your app.js where you configure express you would do this:
var app = express();
app.locals.config = require('./config')
app.get('/', function(req, res) {
return res.render('index')
})
Problem
Ive a nodejs app with express-handlebars and i am wanting to define variable for things like the 'host' address for CSS and Javascript that are currently being imported in a header.hbs file that i call form within the specific layout. Ive created a config.js file which has a number of variables i want to set and ive imported that into the app.js using: ``` var config = require('./config.js'); ``` but then im lost as t where to go. for example i was thinkging if i can some how do something like this: ``` <link href="{{config.csshost}}basev1.css" rel="stylesheet" type="text/css" /> ``` Can anyone provide some pointers, am stumped other than declaring these variable every time i load the template.