shannonmoeller / handlebars-layouts

Handlebars helpers which implement layout blocks similar to Jinja, Nunjucks (Swig), Pug (Jade), and Twig.
http://npm.im/handlebars-layouts
MIT License
361 stars 29 forks source link

weird issue with blocks #8

Closed mbouclas closed 10 years ago

mbouclas commented 10 years ago

super simple scenario, works without express (by doing a manual compile and template()) but when using res.render i get the following error :

TypeError: /media/sf_work/testing/partial/App/views/partials/dashboard.hbs: Cannot read property 'body' of undefined
   at getBlocks (/media/sf_work/testing/partial/node_modules/handlebars-layouts/index.js:6:18)
   at Object.handlebars.registerHelper.block (/media/sf_work/testing/partial/node_modules/handlebars-layouts/index.js:59:26)
   at Object.eval (eval at <anonymous> (/media/sf_work/testing/partial/node_modules/hbs/node_modules/handlebars/dist/cjs/handlebars/compiler/javascript-compiler.js:189:23), <anonymous>:13:161)
   at /media/sf_work/testing/partial/node_modules/hbs/node_modules/handlebars/dist/cjs/handlebars/runtime.js:86:31
   at /media/sf_work/testing/partial/node_modules/hbs/node_modules/handlebars/dist/cjs/handlebars/compiler/compiler.js:465:21
   at /media/sf_work/testing/partial/node_modules/hbs/lib/hbs.js:80:17
   at /media/sf_work/testing/partial/node_modules/hbs/lib/hbs.js:61:11
   at Function.Waiter.done (/media/sf_work/testing/partial/node_modules/hbs/lib/async.js:60:16)
   at /media/sf_work/testing/partial/node_modules/hbs/lib/hbs.js:56:15
   at fs.js:266:14

Layout :

<div id="wrapper">
    {{#block "body"}}

    {{/block}}
</div>

partial :

{{#extend "layout"}}
    {{#append "body"}}
asdasdasd
    {{/append}}
{{/extend}}

Like i mentioned earlier, when doing

app.get('/' ,function (req, res) {
  res.render('partials/dashboard',{
    include_file : 'index',
    location : 'home'
  });
});

i get the above error. If i do a

      hbs.registerPartial('layout', fs.readFileSync(__dirname + '/views/layout.hbs', 'utf8'))
        var template = hbs.compile(hbs,handlebars.partials['partials/dashboard']);

app.get('/' ,function (req, res) {
  res.send(template({
            title: 'Layout Test',
            items: [
                'apple',
                'orange',
                'banana'
            ]
        }));
});

works like a charm, i get the correct output. One weird thing i noticed is that on the Express scenario, doing a console.log in the getBlocks function of the var blocks i get the following output

{}
{ body: [ { should: 'append', fn: [Object] } ] }
undefined

catching the undefined solved the error but the partial is not added to the layout. I am using the 0.3.3 version and 4.8.8 of express

Any help would be most welcomed at this point.

shannonmoeller commented 10 years ago

Thanks for submitting this issue. I'll be looking into Express compatibility issues shortly. My first task is to setup a proper test case.

shannonmoeller commented 10 years ago

I've added a test case to the deep-inheritance feature branch and it's working flawlessly for me. Can you take a look?

https://github.com/shannonmoeller/handlebars-layouts/blob/f35bf3597da148b269b41989d6be4e5349ee4a78/test/express.js?ts=4

shannonmoeller commented 10 years ago

Version 1.0.0 released.