0

in my project I am using Symfony 4.3 with Twig and javascript with jquery.

I need to pass a json encoded object from php to javascript, but it gets broken with double quotes. For example, on php side I do the following:

$new_obj = new \stdClass;
$new_obj->value = 'Some data with "double " quotes';
print_r(json_encode($new_obj));exit;

return $this->render('mytemplate.html.twig', [
    'new_obj' => $new_obj     
]);

So here print_r(json_encode($new_obj)) gives the following:

{"value":"Some data with \"double \" quotes"}

and this looks loke valid json because double qoutes get escaped with slashes. But when I get it in my twig template, I receive the following:

{"value":"Some data with "double " quotes"}

So double quotes are replaced with " and escaping slash gets removed at all. I can restore encoded quotes back with the following code:

function htmlDecode(input){      
    var doc = new DOMParser().parseFromString(input, "text/html");
    return doc.documentElement.textContent;
} 

but how can I make my json valid again? JSON.parse() says

Uncaught SyntaxError: Unexpected token d in JSON at position 26
at JSON.parse (<anonymous>)

[![pic][1]][1]

and I have alreadt tried twig internal {{ data|json_encode() }} function on my raw object, or json_encode($new_obj, JSON_HEX_QUOT) but it did not help.

Any ideas how to fix it would be welcome. Thank you.

Jack
  • 857
  • 14
  • 39
  • Possible duplicate of [Use Javascript to access a variable passed through Twig](https://stackoverflow.com/questions/13928729/use-javascript-to-access-a-variable-passed-through-twig) – Yassine CHABLI Oct 25 '19 at 12:37

1 Answers1

0

You could either turn your variable into a json before passing it to twig :

$new_obj = new \stdClass;
$new_obj->value = 'Some data with "double " quotes';

return $this->render('mytemplate.html.twig', [
    'new_obj' => json_encode($new_obj)     
]);

And dumping new_obj would show you :

{"value":"Some data with \"double \" quotes"}

Or you could json_encode it using twig directly inside the page :

{{ new_obj|json_encode|raw }}
Dylan KAS
  • 4,840
  • 2
  • 15
  • 33
  • thats what Im actually doing, but it gives me only `{"value":"Some data with "double " quotes"}` – Jack Oct 25 '19 at 12:49
  • Even using the |raw filter ? – Dylan KAS Oct 25 '19 at 12:55
  • yes, even with `|raw` filter. weird behaviour: if I see sources of the page, I see my json WITH escaping slashes, but when in console - without them – Jack Oct 25 '19 at 13:00
  • Can you show us how you use your twig variable to pass it to JS ? There may be a problem there – Dylan KAS Oct 25 '19 at 13:03
  • I got it working (`|raw` filter works). Everything we need to do is `var myVar = {{ newObj|raw }}` WITHOUT quotes. I was wrapping already JSON object into it (maybe here my slashes were cut out), and then I was trying to parse it once again. You can add this info into your answer and I will accept it. Thank you – Jack Oct 25 '19 at 13:14
  • Your welcolme, that's why I said to directly use `{{ new_obj|json_encode|raw }}`, feel free to validate the answer if it was enough – Dylan KAS Oct 25 '19 at 13:54