Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
109
rated 0 times [  112] [ 3]  / answers: 1 / hits: 72908  / 12 Years ago, tue, december 18, 2012, 12:00:00

I have a controller that passes an array to a twig template, which I want to use in a script written on that page. How would I go about doing that?



I've tried this in my .twig template:



<script>
$(document).ready(function(){
var test = {{ testArray }};
});
</script>


but that only works if it's a string.


More From » php

 Answers
74

You might have to json_encode the array, try this:


<script>
$(document).ready(function(){
var test = {{ testArray|json_encode(constant('JSON_HEX_TAG'))|raw }};
});
</script>

[#81365] Sunday, December 16, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
anders

Total Points: 295
Total Questions: 106
Total Answers: 104

Location: Angola
Member since Wed, Apr 13, 2022
2 Years ago
;