Reputation: 5612
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 = [{"label":"CN Clogs Beach","data":5},{"label":"Prod my prod","data":5},{"label":"New Pro","data":3}];
Upvotes: 2
Views: 1932
Reputation: 2106
Try
<script type="text/javascript">
var data = {{ jsonArray | raw }};
</script>
or
var data = $.parseJSON('{{ jsonArray | raw }}');
Upvotes: 3