简体   繁体   中英

Send variables from Symfony2 PHP file to js file

I'm really new to JavaScript and I could't fine some tutorials about this. If there are ones, please tell me to read them.

What I want to do is pass variables from my PHP controller to a .js file - I want to populate Highcharts variables.

I know that I can send response, but I need to load a template, too.

This is the template:

...

{% block body %}

 <h1>Months</h1>

 // This is the Chart:    
 <div id="container" style="width:100%; height:400px;"></div>

    {%block javascript%}
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <script src="http://code.highcharts.com/highcharts.js"></script>

        <script src="{{ asset('bundles/acmebudgettracker/js/month.js') }}"></script>
    {%endblock%}

{% endblock %}

The .js file called month.js

      $(function () { 
$('#container').highcharts({
    chart: {
        type: 'bar'
    },
    title: {
        text: 'Budget for months'
    },
    xAxis: {
        categories: ['Spent', 'Saved']
    },
    yAxis: {
        title: {
            text: 'Fruit eaten'
        }
    },
    series: [{
        name: 'Jane',
        data: [1, 0, 4]
    }, {
        name: 'John',
        data: [5, 7, 3]
    }]
});

});​

And the controller:

public function monthsAction()
{
    $this->setUser();
    $month_repository = $this->setRepository('Month');
    $month = new Month();
    $form = $this->createForm(new MonthType(), $month);

    $all_months = $month_repository->findByUser($this->user); 

    return $this->render('AcmeBudgetTrackerBundle:Months:months.html.twig', array(
        'all_months' => $all_months,
        'form' => $form->createView()
    ));
}

What I want to do is to give variables from the controller to month.js and still be able to show the template. Any ideas or tutorials how to achieve this? Thanks in advance!

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);
});​

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM