Howto generate json with smarty?

Sinan picture Sinan · Aug 17, 2009 · Viewed 47.2k times · Source

In Smarty, is there a standard function or an easy way to generate json from an array, as json_encode() does in php?

I could not see it in Smarty documentation, wanted to ask here.

Answer

Tom Haigh picture Tom Haigh · Sep 15, 2009

This should work. The @ makes smarty run the modifier against the whole array, otherwise it does it for each element.

{$myarray|@json_encode}

If $escape_html is enabled, you will need to use nofilter:

{$myarray|@json_encode nofilter}