web-development-with-node-and-express icon indicating copy to clipboard operation
web-development-with-node-and-express copied to clipboard

Handlebars not seeing "partials.weather.locations"

Open Talamantez opened this issue 10 years ago • 13 comments

Hi -

I'm working on the weather widget example. The template renders, but it does not render the Each Block. A console.dir(partials) on the template returns "partials is not defined". I've reduced the weather locations array to a single location and name for testing.

weather.handlebars:

<div class="weatherWidget">

          {{#each partials.weather.locations}}
              <h3>{{name}}</h3>
              {{/each}}

      {{! Below code is rendering}}
          <small>Source: <a href="http://www.wunderground.com">Weather Underground</a></small>
</div>

Thanks! Robert

Talamantez avatar Oct 28 '14 23:10 Talamantez

Nothing is jumping out at me, and I won't be at a computer to dig into this until tomorrow morning. In the meantime, you might want to Google "handlebars debugging". There are some useful techniques for dumping the entire context in the view, which is really the last stop in the templating chain: it's useful to work backwards. If you can't figure it out, I will look at it in the morning.

Best, Ethan

On Tuesday, October 28, 2014, Robert Talamantez [email protected] wrote:

Hi -

I'm working on the weather widget example and can't seem to get it to render. I've reduced the weather locations array to a single location and name for testing.

weather.handlebars:

{{! Below code is not rendering}} {{#each partials.weather.locations}}

{{name}}

{{/each}} {{! Below code is rendering}} Source: Weather Underground

home.handlebars:

Welcome to Meadowlark Travel!

{{> weather}}

This is the related code I have in meadowlark.js:

function getWeatherData(){ return{ locations: [ { name: 'Portland' }, ], }; }

app.use(function(req, res, next){ if(!res.locals.partials) res.locals.partials = {}; res.locals.partials.weather = getWeatherData(); console.dir(res.locals.partials.weather); next(); });

Please let me know if you see anything obvious in this code that would stop it from rendering. Any other advice is also welcome.

Thanks! Robert

Reply to this email directly or view it on GitHub https://github.com/EthanRBrown/web-development-with-node-and-express/issues/9 .

EthanRBrown avatar Oct 28 '14 23:10 EthanRBrown

Hi Ethan - Thanks for taking a look - I refined the ticket to make it a little clearer. Thanks for the tip - I'll post if i figure it out.

-Roberto

Talamantez avatar Oct 28 '14 23:10 Talamantez

Hi @Talamantez, This is happening also to me, did you figured out the reason? I have been looking the whole afternoon and I can not find anything. Maybe because I am using express-handlebars instead of express3-handlebars?

Thanks in advance

usmany avatar Mar 07 '16 23:03 usmany

Sorry to let this drop...let me take a look now.

EthanRBrown avatar Mar 07 '16 23:03 EthanRBrown

Thanks Ethan! I have been loving your book a lot!! Thanks

usmany avatar Mar 07 '16 23:03 usmany

Gentlemen, without some source code, I can't really tell what's going on. It's working out of the box for me in the repo, so there must be some difference in your repo, or perhaps a platform-specific issue. It's not because you're using express-handlebars instead of express3-handlebars, @usmany, that was just a rename for the newer version of it. If either of you could share your repo with me, or zip up your code, I'll take a look.

EthanRBrown avatar Mar 07 '16 23:03 EthanRBrown

Thanks, @usmany !

EthanRBrown avatar Mar 07 '16 23:03 EthanRBrown

Ethan, Excuse me please, I will do it tomorrow as I have to get out now for work :(

usmany avatar Mar 07 '16 23:03 usmany

No problem, I look forward to seeing it!

EthanRBrown avatar Mar 07 '16 23:03 EthanRBrown

Hi Ethan, Here is my github repo https://github.com/usmany/learnNodeExpress.git I am a total newbie in all of this, I don't know if there is a better way to share it. Thanks for your time

usmany avatar Mar 08 '16 20:03 usmany

I must be doing something very wrong and I can not find what it is. I continued with the sections part and they are not rendered neither. It is like if the handlebars tags are processed but at the same time ignored.

usmany avatar Mar 09 '16 21:03 usmany

Alright, I will take a look at this this afternoon...we'll get it figured out!

/e

On Wed, Mar 9, 2016 at 1:22 PM, Many [email protected] wrote:

I must be doing something very wrong and I can not find what it is. I continued with the sections part and they are not rendered neither. It is like if the handlebars tags are processed but at the same time ignored.

— Reply to this email directly or view it on GitHub https://github.com/EthanRBrown/web-development-with-node-and-express/issues/9#issuecomment-194511776 .

EthanRBrown avatar Mar 09 '16 21:03 EthanRBrown

Hi, @usmany,

There were two problems with your code. The first was due to some changes to express-handlebars after I published the book. They added the feature whereby you could provide a new template for partials by setting a property in res.locals.partials. For example, if your partial is called weather, and you set res.locals.partials.weather, instead of passing that along as context, it passes it along as the template! So it breaks the code I originally had in the book. It's been corrected in subsequent printings, and the repo has been updated. The solution is easy: just use a name that's different from the partial itself. So I renamed it to res.locals.partials.weatherContext, and it works.

The second is that you had the middleware that injects the partial data at the very bottom of your middleware chain, right before the 404 handler. For the partial context injection to work, that middleware has to be higher in the chain than where it's used. So I moved it to the top of your chain, and voilà! There are your partials.

EthanRBrown avatar Mar 10 '16 00:03 EthanRBrown