0

I have JSON String in PHP.

Index.php

$myObj = "{ "name":"John", "age":31, "city":"New York" }";
$smartyVars['MyObject'] = filter_var($myObj, FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES);

I am using MyObject var in tpl file with Js.

index.tpl

<script>
var myObj  = "{$MyObject}"
$("input#valDef").val(myObj);
</script>

I want to use escape in tpl file

var myObj  = "{$MyObject|escape}"

After including escape how js give an error and my JS Distorted. my code not working properly. Without escaping its working fine.

But for the security concern, I want to add escape in smarty template and I already SANITIZE my JSON in PHP as you see above the PHP code.

So Please help how to add and work properly.

Abid Hussain
  • 7,724
  • 3
  • 35
  • 53
  • 1
    why are you building your json yourself? why not `json_encode()`? the built-in function does everything for you. smarty even includes a filter for that: `{$myObj|@json_encode}` – Franz Gleichmann Feb 21 '18 at 17:59
  • @ Sammitch How can mark as a duplicate I am not converting array object in JSON. I have Pure JSON. {$myObj|@json_encode} this code convert array to JSON. If I am not wrong. – Abid Hussain Feb 22 '18 at 07:04
  • you have _variables_. which you can _put_ in an _array_. which you then can convert into json. which is the clean way to do this. – Franz Gleichmann Feb 22 '18 at 10:21

0 Answers0