最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Send variables from Symfony2 PHP file to js file - Stack Overflow

programmeradmin4浏览0评论

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=".8.2/jquery.min.js"></script>
    <script src=".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!

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!

Share Improve this question asked Jun 14, 2013 at 8:07 FaeryFaery 4,65012 gold badges52 silver badges96 bronze badges
Add a comment  | 

2 Answers 2

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

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>
发布评论

评论列表(0)

  1. 暂无评论