views:

812

answers:

2

Hi,

In Smarty, is there a standard function or an easy way to parse json from an array, as json_encode() does in php? Actually It seems there is not in smarty documentation but wanted to ask anyways.

Thanks, Sinan.

+1  A: 

I don't know of any. You could assign the json_encode()'s result to a smarty variable in your 'php code' with $smarty->assign( ... ), and then use it in your template.

Also there is a Smarty extension for json_decode(). It shouldn't be hard to write your own extension for the opposite based on this.

Zed
I thought there would be a way like {$var|json_encode}, Thanks anyways.
Sinan Y.
+1, the extension idea is good.
Sinan Y.
+5  A: 

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

{$myarray|@json_encode}
Tom Haigh
Thanks a bunch!
bluej100