20

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.

Sinan
  • 11,443
  • 7
  • 37
  • 48
  • Smarty is translated into PHP after all... so why not save a step, right ? :) – Zed Aug 16 '09 at 22:52
  • 6
    @Al, comment flagged, requirements are set by companies we work for, not by mere mortals like us. and dealing with comments like yours is worse than those requirements. cheers. – Sinan Aug 17 '09 at 20:06

6 Answers6

92

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}
Abhi Beckert
  • 32,787
  • 12
  • 83
  • 110
Tom Haigh
  • 57,217
  • 21
  • 114
  • 142
  • 1
    I didn't know about the @ modifier! This answered the question. – karnage Nov 17 '11 at 20:47
  • As a note, to pass args you need the numerical value. Ie, JSON_UNESCAPED_SLASHES is 64. So, `{$myarray|@json_encode:64 nofilter}` would leave slashes un-escaped. http://stackoverflow.com/a/27806269/2418655 – dhaupin May 02 '17 at 17:43
11

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}
m_katsifarakis
  • 1,777
  • 1
  • 21
  • 27
  • This only applies if your Smarty class has $escape_html set to true. I mean, it SHOULD be if you're doing it right, but still. – Andrew Feb 17 '15 at 22:19
6

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}
progNewbie
  • 4,362
  • 9
  • 48
  • 107
Kanagu
  • 61
  • 1
  • 1
2

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 '&quot;' with ":

{literal}const my_javascript_array = JSON.parse(`{/literal}{json_encode($myarray)|escape:'javascript'}{literal}`.replaceAll('&quot;', '"'));

It is a bit convoluted, but you will thank yourself down the line for learning this trick :)

Spot On
  • 31
  • 5
1
{literal}
<script type="text/javascript">
<!--
var newVar ={/literal}{$myarray|@json_encode nofilter};{literal}
// -->
</script>
{/literal}

My solution

Sarath E
  • 396
  • 3
  • 13
0

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
  • 57,028
  • 9
  • 76
  • 100