ericf / express-handlebars

A Handlebars view engine for Express which doesn't suck.
BSD 3-Clause "New" or "Revised" License
2.32k stars 382 forks source link

locals not accessible within partials #136

Open felixmc opened 9 years ago

felixmc commented 9 years ago

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?

andresgarza commented 8 years ago

:+1:

custa1200 commented 8 years ago

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?

andresgarza commented 8 years ago

@custa1200: not sure if something changed, but it seems that we are indeed able to use app.locals in both template and partials in express@4.13.3 and express-handlebars@2.0.1

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 commented 8 years ago

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>
custa1200 commented 8 years ago

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

toptensoftware commented 6 years ago

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}}
roman-16 commented 6 years ago

Please fix this, I need a quick solution :)

alerad commented 6 years ago

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.

WeiGrand commented 5 years ago

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

iplanwebsites commented 5 years ago

Any plans to fix this or clean workarounds?

nsafai commented 5 years ago

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

titoih commented 4 years ago

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

audrew commented 2 years ago

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

UziTech commented 2 years ago

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.