Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7399367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:03:07+00:00 2026-05-29T04:03:07+00:00

I am trying to register helpers with Handlebars to allow iterating over JSON objects.

  • 0

I am trying to register helpers with Handlebars to allow iterating over JSON objects. This gist looked like an appropriate solution. I converted that into the following CoffeeScript. Nothing seems to happen when I use either of the helpers (that holds true for both vanilla JavaScript and the CoffeeScript version). Any ideas?

$ ->
  Handlebars.registerHelper "key_value", (obj, fn)->
    buffer = ""
    key 
    for key in obj 
      if obj.hasOwnProperty(key)
        buffer += fn({key: key, value: obj[key]})
    buffer

  Handlebars.registerHelper "each_with_key", (obj, fn)->
    context
    buffer = ""
    key 
    keyName = fn.hash.key
    for key in obj 
      if obj.hasOwnProperty(key)
        context = obj[key]
        if keyName
          context[keyName] = key 
          buffer += fn(context)
    buffer

In the template:

{{#key_value categories}}
I'M ALIVE!!
{{/key_value}}

{{#each_with_key categories key="category_id"}}
I'M ALIVE!!
{{/each_with_key}}

I am currently using gem 'handlebars-assets' in the Gemfile to add handlebars to a rails app.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T04:03:07+00:00Added an answer on May 29, 2026 at 4:03 am

    Your JavaScript to CoffeeScript transliteration is broken. You don’t use for ... in to iterate over an object in CoffeeScript, you use for k, v of ...:

    Use of to signal comprehension over the properties of an object instead of the values in an array.

    This CoffeeScript loop:

    for x in y
        ...
    

    becomes this JavaScript:

    for (_i = 0, _len = y.length; _i < _len; _i++) {
      x = a[_i];
      ...
    }
    

    So if y is an object without a length property, then _len will be undefined and the JavaScript for(;;) loop won’t iterate at all.

    You should also be using own instead of hasOwnProperty:

    If you would like to iterate over just the keys that are defined on the object itself, by adding a hasOwnProperty check to avoid properties that may be interited from the prototype, use for own key, value of object.

    but that’s more for convenience than correctness.

    Also, CoffeeScript loops are expressions so you’d usually say array = expr for own k, v in o or the equivalent form:

    array = for own k, v in o
        expr
    

    if expr is more than one line or too long to allow for a readable comprehension.

    A correct and more idiomatic version of your helpers in CoffeeScript would look more like this:

    Handlebars.registerHelper "key_value", (obj, fn)->
        (fn(key: key, value: value) for own key, value of obj).join('')
    
    Handlebars.registerHelper "each_with_key", (obj, fn)->
        key_name = fn.hash.key
        buffer   = for own key, value of obj
            value[key_name] = key
            fn(value)
        buffer.join('')
    

    Demo: http://jsfiddle.net/ambiguous/LWTPv/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to register a WebDeleting event receiver within SharePoint. This works fine
I'm trying to register a hot key, I'm translating this C++ code into C#:
I have been trying to register 3 hotkeys. I followed this example (or this
I'm trying to register the NSMouseMoved event globally using this line of code: [NSEvent
I am trying to register a type like IRequestHandler 1[GenericTestRequest 1[T]] which will be
I am trying to register a c++ function in Lua. But getting this error:
I'm trying to register my tumblr website with Facebook in accordance with this tutorial
I am trying to register module in dojo. I register one module in this
I'm trying to register an externally hosted SQL 2000 server through Enterprise Manager which
I'm trying to register an atl service using ExeName.exe /service as described here: http://msdn.microsoft.com/en-us/library/74y2334x(VS.80).aspx

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.