aditya
aditya

Reputation: 996

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

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?

Upvotes: 35

Views: 48402

Answers (3)

Mike
Mike

Reputation: 1811

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)}};

Upvotes: 67

everyman
everyman

Reputation: 3407

For anyone, who has similar problem with Blade / Laravel5.x

var v = JSON.parse('{!! $v !!}');

Upvotes: 4

Tac Tacelosky
Tac Tacelosky

Reputation: 3430

You can add the options in the following way:

{{ data|json_encode(constant('JSON_PRETTY_PRINT'))|raw }}

Adding this because it answers the question in your title, but it sounds like the raw filter was really what you were looking for. Still, others may find this useful.

Upvotes: 44

Related Questions