score:8

Accepted answer

quick & "dirty" answer : set the variable in your twig file using balises, and use those vars in your js file.

<script>
    var foo = {{ bar }};
</script>

score:14

you can generate javascript as twig template:

monthscontroller.php

public function scriptaction()
{
    // some logic
    return $this->render('acmedemobundle:months:script.js.twig', array(
        'user_months' => $user_months
    ));
}

routing.yml

javascript_route:
  path:     /generated-scripts/month.{_format}
  defaults: { _controller: acmedemobundle:article:script, _format: js }
  requirements:
      _format:  js|json

script.js.twig

$(function () {
    var options = {
        chart: {
            type: 'bar'
        },
        title: {
            text: 'budget for months'
        },
        xaxis: {
            categories: ['spent', 'saved']
        },
        yaxis: {
            title: {
                text: 'fruit eaten'
            }
        },
        series: []
    };

    {% for user in user_months %}
    options.series.push({ name: '{{ user.name }}', data: [{{ user.months|join(',') }}] };);
    {% endfor %}

    $('#container').highcharts(options);
});​

Related Query

More Query from same tag