express-handlebars
express-handlebars copied to clipboard
locals not accessible within partials
My setup looks something like the following:
var hbs = handlebars.create({
extname: '.hbs'
});
app.locals['static-server'] = 'https://' + config.services.assets.hostname;
app.set('views', app.path('views'));
app.engine('hbs', hbs.engine);
app.set('view engine', 'hbs');
The static-server
local works properly within views, but whenever it is used within a partial, the variable is always undefined. Is this a bug, or do I have something configured incorrectly?
:+1:
I also have encountered this issue, and it seems like an absolute show stopper for using express-handlebars. Love the way it is supposed to work but right now it's definitely sucking that some of the basics are failing.
I have also opened a bug #153. Did either @felixmc or @andresgarza work out any workarounds?
@custa1200: not sure if something changed, but it seems that we are indeed able to use app.locals in both template and partials in [email protected]
and [email protected]
Here's a sample app.js that works for me:
var express = require('express');
var handlebars = require('express-handlebars');
var path = require('path');
var app = express();
app.locals.hello = 'Hello'
app.locals.world = 'World!'
app.set('views', path.join(__dirname, '/views'));
app.engine('handlebars', handlebars({
defaultLayout: 'default',
layoutsDir: 'views/layouts',
partialsDir: 'views/partials'
}));
app.set('view engine', 'handlebars');
app.use(function (req, res, next) {
res.render('hello');
})
app.listen(3000);
Here's my dir structure:
.
├── app.js
└── views
├── hello.handlebars
├── layouts
│ └── default.handlebars
└── partials
└── world.handlebars
hello.handlebars
<h1>{{hello}} {{> world}}</h1>
partials/world.handlebars
{{world}}
layouts/default.handlebars
<!DOCTYPE html>
<html>
<head>
<title>Sample App</title>
<meta charset="utf-8">
</head>
<body>
{{{body}}}
</body>
</html>
Aren't app.locals supposed to be {{@hello}} not {{hello}} like your example?
I too am still seeing this but only when passing a sub object to the partial.
@andresgarza - your example works because you're not passing a sub-object and the original express options object (which is already decorated with the locals) is just getting passed through. If you try this I think you'll find it won't work:
<h1>{{hello}} {{> world someSubObjectOfTheCurrentContextObject}}</h1>
Locals are also dropped in cases like this:
{{#each items}}
{{> renderItem}}
{{/each}}
Please fix this, I need a quick solution :)
I doubt this comment will help everybody, but make sure the app.locals value you are trying to access is actually correctly set (Either by debugging, console.log, etc), I just tried using it on a partial and worked perfectly.
While in a for each loop, {{@root.yourLocalsDataKey}} may help
Any plans to fix this or clean workarounds?
Confirming I experienced the same issue, unable to access a {{user}}
(res.locals.user) inside an {{#each array as |item|}}
@WeiGrand 's solution worked for.
{{@root.user.name}}
While in a for each loop, {{@root.yourLocalsDataKey}} may help
thanks a lot, not found it in stackoverflow, nor development forums. Nice solution, help a lot :)
Just one comment: is any problem to use app.locals instead of res.locals? because it works for me with app.locals too happy coding
{{@user.email}} doesn't help too. any other solutions? Can't find why hbs not rendering locals
All new development of express-handlebars is done on a new repo express-handlebars/express-handlebars. Please create an issue there if this is still an issue.