Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use php json_encode options in twig file with json_encode twig function

Tags:

php

twig

symfony

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?

like image 666
aditya Avatar asked Sep 04 '12 06:09

aditya


People also ask

What does the PHP function json_encode () do?

The json_encode() function is used to encode a value to JSON format.

What is json_encode and Json_decode in PHP?

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.


1 Answers

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)}}; 
like image 83
Mike Avatar answered Sep 19 '22 11:09

Mike