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

前端 未结 3 412
星月不相逢
星月不相逢 2020-12-29 19:22

I am trying to use twig json_encode function but when I do this

    var packageDetails =  {{(packageDetails|json_encode)}};

and packageDeta

相关标签:
3条回答
  • 2020-12-29 20:12

    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)}};
    
    0 讨论(0)
  • 2020-12-29 20:16

    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.

    0 讨论(0)
  • 2020-12-29 20:25

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

    var v = JSON.parse('{!! $v !!}');
    
    0 讨论(0)
提交回复
热议问题