Search code examples
jsondust.js

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


Solution

  • 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.