0

Hello guys i am trying to pass an array to a js file but i have this error :

An exception has been thrown during the rendering of a template ("Notice: Array to string conversion").

Soo this what i want : data: ['2017/7','2017/8']

so i do it like this (in twig) : data: '{{ user_month_month }}'

And when i vardump user_month_month this is what i get :

array(2) { [0]=> string(6) "2018-7" [1]=> string(6) "2018-8" }

So i think that i don't call it well in the js part, what should i do ?

if you want there is my controller :

public function GraphicShow()
{
    $em = $this->getDoctrine()->getManager();

    $users = $em->getRepository('AppBundle:User')->countUsers();
    $not_logged = $em->getRepository('AppBundle:User')->countNotActiveUsers();
    $logged = $em->getRepository('AppBundle:User')->countActiveUsers();
    $user_month_months = $em->getRepository('AppBundle:Profile')->countByMonthMonth();
    $user_month_totals = $em->getRepository('AppBundle:Profile')->countByMonthTotal();

    $array_totals = array();
    $array_months = array();

    foreach($user_month_totals as $user_month_total) {
        $array_totals[] = intval($user_month_total["total"]);
    }

    foreach ($user_month_months as $user_month_month) {
        $array_months[] = $user_month_month["month"];
    }

    $not_logged_result = $not_logged["number"] / $users["number"] * 100;
    $logged_result = $logged["number"] / $users["number"] * 100;

    var_dump($array_months);
    die();

    return $this->render('admin/user/pie_stats.html.twig', array(
        'user_month_month' => $array_months,
        'user_month_total' => $array_totals,
        'user_not_logged' => $not_logged_result,
        'user_logged' => $logged_result,
        'users' => $users,
        'loggedAs' => $this->getUser(),
        'alert' => 0,
    ));
}

ps / edit : the date is for a graphic and it looks like this :

enter image description here

Hope that i explain well, thx for all who will try to answer :p

With DarkBee method i got this :

enter image description here

3 Answers 3

7

The fastest way to pass an array from twig to javascript is by converting the array to JSON, twig has a builtin filter json_encode for this:

data: {{ user_month_month | json_encode | raw }}
Sign up to request clarification or add additional context in comments.

4 Comments

Not working ... (ok that's working like there is no error) but got this : ["2018-7uot;,uot;2018-8uot;] and like i do this for a graphic the first value should be 2018-7 the second 2018-8 ... but with this way the first value in "[" the second "&", the third "q" ...
there is the same thing ... like this is better because now i see the graphic but i will edit the post and that you can see what it does
ok done i change the photo if you wanna see (like it delete the "quot" word but always one row for one letter)
You don't need the surrounding single quotes around the statement ofcourse otherwise you are dealing with a string and not an array
2

The recommended way by symfony for passing information from twig to javascript is by storing information in data attributes and reading them later in JavaScript. Documentation here.

In your case that you have an array you should use this to work as expected:

<div id="your-div" data-yourArray="{{ yourArray|json_encode|e('html_attr') }}">

then use JSON.parse to deserialize your array in javascript.

DarkBee's answer does not include the use of the escape filter, that's why it doesn't work as expected.

In your javascript code:

var yourDiv = document.getElementById('your-div');
var yourArray = JSON.parse(yourDiv.dataset.yourArray); 

2 Comments

and i put this : var months = document.getElementById('month_data'); in the js file and i do data: months (i put the id to month_data for sure)
You should use: var monthsDiv = document.getElementById('months_div'); to get the div on which you added the "data-months" attribute as I mentioned in my answer (in my answer I named it data-yourArray). Then you use: var months = JSON.parse(monthsDiv.dataset.months); to get months as a javascript array.
1
<input type="text" id="txt"> <!-- // 2017/7 2017/8 -->

<script>
    date = '{{ user_month_month[0] ~ ' ' ~ user_month_month[1] }}';
    alert(date); // 2017/7 2017/8
    txt = document.getElementById('txt');
    txt.value = "{{ user_month_month[0] ~ ' ' ~ user_month_month[1] }}";
</script>

1 Comment

print nothing ... and like i would like to be automatic

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.