EthanRBrown / web-development-with-node-and-express

Companion repository to Web Development With Node and Express, first edition.
1.02k stars 502 forks source link

Handlebars not seeing "partials.weather.locations" #9

Open Talamantez opened 9 years ago

Talamantez commented 9 years ago

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

EthanRBrown commented 9 years ago

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 notifications@github.com 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 .

Talamantez commented 9 years ago

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

usmany commented 8 years ago

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

EthanRBrown commented 8 years ago

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

usmany commented 8 years ago

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

EthanRBrown commented 8 years ago

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

Thanks, @usmany !

usmany commented 8 years ago

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

EthanRBrown commented 8 years ago

No problem, I look forward to seeing it!

usmany commented 8 years ago

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

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.

EthanRBrown commented 8 years ago

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 notifications@github.com 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 commented 8 years ago

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.