abernier
abernier

Reputation: 28208

JSON stringify helper

Considering the following context:

{
  dogs: [ {name: "rex"}, {name: "tobi"} ]
}

How can I dump dogs as an array, ie: something similar to JSON.stringify(dogs)

I tried with {#dogs}{@contextDump}{/dogs}, but (logically) it outputs:

 {"name": "rex"}{"name": "tobi"}

rather than:

["name": "rex"}, {"name": "tobi"}]

Thanks

Upvotes: 4

Views: 2853

Answers (1)

smfoote
smfoote

Reputation: 5609

This is possible using filters. Your template would look like this:

{dogs|js|s}

js is basically JSON.stringify, and s unescape everything, which will unescape the quotes in the JSON.

See this jsFiddle.

Upvotes: 10

Related Questions