phpsmartyjson

Howto generate json with smarty?


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.


Solution

  • Now deprecated

    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}