javascriptcoffeescriptunderscore.jseco

How to print result of _each method in eco template


I'm sure I'm doing pretty simple wrong but can't seem to find an explanation. I've got the following line in my template which is not printing any values to the html output:

  <%= _.each(@place.get("hash"),(count, tag) -> "#{tag} ") %>

This line is printing values to the console perfectly fine:

  <%= _.each(@place.get("hash"),(count, tag) -> console.log "#{tag} ") %>

When I try using a print command and refresh, google chrome throws up a print menu. How can I resolve this


Solution

  • Underscore's each doesn't return anything so <%= _.each(...) %> doesn't do anything useful. You could use _.map and join:

    <%= _(@place.get('hash')).map((count, tag) -> tag).join(' ') %>
    

    or you could use _.keys and join:

    <%= _(@place.get('hash')).keys().join(' ') %>
    

    Your _.each is just extracting the keys so you should say what you mean.

    If you're using node.s then you should have Object.keys as well:

    <%= Object.keys(@place.get('hash')).join() %>