Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON stringify helper

Tags:

json

dust.js

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

like image 919
abernier Avatar asked Dec 25 '22 19:12

abernier


1 Answers

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.

like image 112
smfoote Avatar answered Jan 07 '23 09:01

smfoote