2

I trying to echo json encode string in twig. This is my code,

Controller

$jsonArray = array(
            array(
                'label' => 'CN Clogs Beach',
                'data' => 5
            ),
            array(
                'label' => 'Prod my prod',
                'data' => 5
            ),
            array(
                'label' => 'New Pro',
                'data' => 3
            )
        );

        $jsonArray = json_encode($jsonArray);

        return $this->render("EagleAdminBundle:dashboard:index.html.twig", array(                        
                    'jsonArray' => $jsonArray
        ));

This is twig file

<script type="text/javascript">
var data = {{ jsonArray }};
</script>

Instead of getting json array I get something like this,

var data = [{&quot;label&quot;:&quot;CN Clogs Beach&quot;,&quot;data&quot;:5},{&quot;label&quot;:&quot;Prod my prod&quot;,&quot;data&quot;:5},{&quot;label&quot;:&quot;New Pro&quot;,&quot;data&quot;:3}];

1 Answer 1

3

Try

<script type="text/javascript">
var data = {{ jsonArray | raw }};
</script>

or

var data = $.parseJSON('{{ jsonArray | raw }}');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.