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.
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.
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}
While {$myarray|@json_encode}
does in fact emit the array encoded in json, it also escapes special characters, making the array unusable in javascript.
To avoid escaping special characters and also be able to use the array in javascript use the nofilter flag:
{$myarray|@json_encode nofilter}
You have to use json_encode()
in your php code then assign the value to smarty using
$smarty->assign()
function. After that you have to parse that value in your template file using
javascript.
code snippet:
{literal}
<script>
var json = JSON.parse('{/literal}{$your_json_encoded_array}{literal}');
//another statement
</script>
{/literal}
While {$myarray|@json_encode nofilter}
will work, there is a security hole here since we are doing variable escaping. Variable escaping (via nofilter
) is highly discouraged because malicious code can be displayed and executed easily.
There is another approach to consider, using the 'javascript'
escaping and replacing '"'
with "
:
{literal}const my_javascript_array = JSON.parse(`{/literal}{json_encode($myarray)|escape:'javascript'}{literal}`.replaceAll('"', '"'));
It is a bit convoluted, but you will thank yourself down the line for learning this trick :)
{literal}
<script type="text/javascript">
<!--
var newVar ={/literal}{$myarray|@json_encode nofilter};{literal}
// -->
</script>
{/literal}
My solution
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.