HBS module on Express 3.0 adding text to the generated HTML


I'm getting this bug that I'm fairly confused about. I'm running an express 3.0.6 app using the hbs package for handlebars templating. I'm trying to create blocks for the scripts and stylesheets for each view, based off this example.

For some reason, whenever I try to use the helper, it adds some numbers (as straight text) to the top of the html file. Even weird, the numbers change upon refreshing, and I have no idea what's wrong or even how to figure out what's going on.

blocks = {}
hbs.registerHelper 'extend', (name, context) ->
    block = blocks[name]
    if not block
        block = blocks[name] = []
    block.push(context.fn this)

hbs.registerHelper 'block', (name) ->
    val = (blocks[name] || []).join('\n')
    blocks[name] = []
    return val

In the layout.html:

  {{{block "stylesheets"}}}
  {{{block "javascripts"}}}

In other views:

{{#extend "stylesheets"}}
  <link ... />

{{#extend "javascripts"}}
  <script ... 

In the output html, I get "1 1 ". The 1's sometimes randomly change in value, but there are always x of them, where x is the number of extend tags.

Thanks for any insight/help anyone might be able to give!

EDIT:: Also, another thing I noticed is that if I change the block helpers to:


the numbers start at 1, and then keep incrementing by 1 each time I refresh the page. I'm not sure what's going on, but hopefully that'll help.

Problem courtesy of: dxu


Turns out in the 'extend' register helper, coffeescript automatically returned the output of the last statement of the method, and that was causing an issue. If I just stick in an extra return statement in there, it worked.

Solution courtesy of: dxu


There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.