I am trying to use twig json_encode function but when I do this
var packageDetails = {{(packageDetails|json_encode)}};
and packageDetails is an array of array passed from controller
It gives me error saying
invalid property id
because of "
so I want to use escape filter; how do I use it?
The json_encode() function is used to encode a value to JSON format.
JSON data structures are very similar to PHP arrays. PHP has built-in functions to encode and decode JSON data. These functions are json_encode() and json_decode() , respectively. Both functions only works with UTF-8 encoded string data.
Is it simply because you are not wrapping your output in quotes?
var variable = '{{{reference}}}';
Update:
The actual answer to solve the question was adding |raw to the tag as per comments
var packageDetails = {{(packageDetails|json_encode|raw)}};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With