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?
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.
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!
issue closed
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.
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 <
[email protected]
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:
https://github.com/janl/mustache.js/issues/146#issuecomment-3100397
Most helpful comment
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 bex,y,z
.If you want anything different, you need to write the template manually. Something like:
Hope that helps.