express-handlebars icon indicating copy to clipboard operation
express-handlebars copied to clipboard

locals not accessible within partials

Open felixmc opened this issue 9 years ago • 14 comments

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?

felixmc avatar Jun 23 '15 00:06 felixmc

:+1:

andresgarza avatar Oct 05 '15 23:10 andresgarza

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 avatar Nov 14 '15 23:11 custa1200

@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);

andresgarza avatar Nov 18 '15 23:11 andresgarza

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>

andresgarza avatar Nov 18 '15 23:11 andresgarza

Aren't app.locals supposed to be {{@hello}} not {{hello}} like your example?

custa1200 avatar Nov 23 '15 13:11 custa1200

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}}

toptensoftware avatar Sep 03 '17 08:09 toptensoftware

Please fix this, I need a quick solution :)

roman-16 avatar Dec 10 '17 20:12 roman-16

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.

alerad avatar Dec 23 '17 08:12 alerad

While in a for each loop, {{@root.yourLocalsDataKey}} may help

WeiGrand avatar Jul 22 '18 04:07 WeiGrand

Any plans to fix this or clean workarounds?

iplanwebsites avatar Oct 29 '18 14:10 iplanwebsites

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}}

nsafai avatar Nov 02 '18 04:11 nsafai

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

titoih avatar Jan 27 '20 23:01 titoih

{{@user.email}} doesn't help too. any other solutions? Can't find why hbs not rendering locals

audrew avatar Sep 06 '21 01:09 audrew

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.

UziTech avatar Sep 06 '21 02:09 UziTech