Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing Arrays or Objects to Helpers #1455

Open
doc987 opened this issue Jul 14, 2018 · 6 comments
Open

Passing Arrays or Objects to Helpers #1455

doc987 opened this issue Jul 14, 2018 · 6 comments
Labels
Milestone

Comments

@doc987
Copy link

doc987 commented Jul 14, 2018

In addition to passing strings, numbers, and boolean values, could it be possible to pass arrays or objects to helpers? For example:

{{helper_function "abc" 10 ["a","b","c"] ["d","e","f"] {"a":1,"b":2,"c":3} true}}
@nknapp
Copy link
Collaborator

nknapp commented Jul 16, 2018

The easiest way is to

  • create a custom helper (e.g. arr) that returns an array from a dynamic lists of param (be sure to omit the last argument, because this is the options-object)
  • Call this helper as in {{helper_function "abc" 10 (arr "a" "b" "c") ... }}`

I have created an example here: https://jsfiddle.net/9D88g/618/

@doc987
Copy link
Author

doc987 commented Jul 18, 2018

Somewhat more typing than an array literal, but works okay. Any suggestions for an object literal?

@nknapp
Copy link
Collaborator

nknapp commented Jul 25, 2018

@wycats wants a formal spec of the existing language before adding new language features (#1277). But currently, nobody is really working on one, so I don't see any progress on this in the near future.

@rcampbel
Copy link

Somewhat more typing than an array literal, but works okay. Any suggestions for an object literal?

Same concept as nknapp's answer for arrays but return the options.hash

@nknapp nknapp added the feature label Apr 5, 2020
@nknapp nknapp added this to the Backlog milestone Apr 5, 2020
@jbsulli
Copy link

jbsulli commented Jun 13, 2020

I ended up going with a json-parse helper and doing {{helper_function (json-parse "[\"a\",\"b\",\"c\"]")}}. Worked well for me because I'm programmatically generating the templates.

@dchenk
Copy link

dchenk commented Feb 1, 2022

You can use the toArray helper in wirechunk/handlebars-helpers if you don't want to write a helper function yourself but can install a library. 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants