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

Arrays converted to strings #146

Closed
barbalex opened this issue Dec 9, 2011 · 5 comments
Closed

Arrays converted to strings #146

barbalex opened this issue Dec 9, 2011 · 5 comments

Comments

@barbalex
Copy link

barbalex commented Dec 9, 2011

I have this array:
"Optionen": [
"x",
"y",
"z"
]

When I call "{{Optionen}}" this is returned: "x,y,z"

But I need to get ["x", "y", "z"]

Is this a bug or am I missing something?

@thegrandpoobah
Copy link
Contributor

Yes, this is by design. Any key that matches in the data object will get coerced into a string if it is not "falsy". In the case of arrays, JavaScript coerces them into strings in the format x,y,z (try it for yourself: open up firebug and type in ["x","y","z"].toString() and the result should be x,y,z.

If you want anything different, you need to write the template manually. Something like:

[{{#Optionen}}"{{.}}",{{/Optionen}}]

Hope that helps.

@barbalex
Copy link
Author

this comes pretty close. Using [{{#Optionen}}"{{.}}", {{/Optionen}}] I get ["x", "y", "z", ]
Unfortunately the last comma will be a problem, when this value is saved again (after for instance having added "a" at the beginning). Is there a way to prevent that last comma?
Oh, after saving, ["a", "x", "y", "z", ] I get this: "["a,x,y,z", ]"
saving a,x,y,z gives the correct Array ["a", "x", "y", "z"]
Looks like I'd better stick to a,x,y,z.
Hope, I never need to save a Value with comma...
Well, I gess there's no easy solution unless not to use mustache.
Correct me it I'm wrong. Closing the issue.
Thanks a lot for helping!

@barbalex
Copy link
Author

issue closed

@thegrandpoobah
Copy link
Contributor

Sorry, not sure what you are trying to do. Not sure what "saving" means, but to eliminate the last comma in Mustache, I think the only way to do it is to introduce a flag variable by pre-iterating the list in JavaScript, which quickly becomes more trouble than its worth. I think the helpers functionality that @janl is building out might make this easier to solve in Mustache itself, but I'm not sure since I'm not too familiar with it.

Alternatively, look at JSON.stringify and JSON.parse (built in to the browser) which may help out in de/serializing arrays into the format you are expecting.

@barbalex
Copy link
Author

I found the solution: .split(", ") did it!
So I load the value with mustache into a field. Then I use .split(", ) when saving the data in the field with saveDoc.

2011/12/12 Sahab Yazdani <
reply@reply.github.com

Sorry, not sure what you are trying to do. Not sure what "saving" means,
but to eliminate the last comma in Mustache, I think the only way to do it
is to introduce a flag variable by pre-iterating the list in JavaScript,
which quickly becomes more trouble than its worth. I think the helpers
functionality that @janl is building out might make this easier to solve in
Mustache itself, but I'm not sure since I'm not too familiar with it.

Alternatively, look at JSON.stringify and JSON.parse (built in to the
browser) which may help out in de/serializing arrays into the format you
are expecting.


Reply to this email directly or view it on GitHub:
#146 (comment)

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

No branches or pull requests

2 participants