Note that there are some explanatory texts on larger screens.

plurals
  1. POSend variables from Symfony2 PHP file to js file
    primarykey
    data
    text
    <p>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.</p> <p>What I want to do is pass variables from my PHP controller to a .js file - I want to populate Highcharts variables.</p> <p>I know that I can send response, but I need to load a template, too.</p> <p>This is the template:</p> <pre><code>... {% block body %} &lt;h1&gt;Months&lt;/h1&gt; // This is the Chart: &lt;div id="container" style="width:100%; height:400px;"&gt;&lt;/div&gt; {%block javascript%} &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"&gt;&lt;/script&gt; &lt;script src="http://code.highcharts.com/highcharts.js"&gt;&lt;/script&gt; &lt;script src="{{ asset('bundles/acmebudgettracker/js/month.js') }}"&gt;&lt;/script&gt; {%endblock%} {% endblock %} </code></pre> <p>The .js file called <code>month.js</code></p> <pre><code> $(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] }] }); </code></pre> <p>});​ </p> <p>And the controller:</p> <pre><code>public function monthsAction() { $this-&gt;setUser(); $month_repository = $this-&gt;setRepository('Month'); $month = new Month(); $form = $this-&gt;createForm(new MonthType(), $month); $all_months = $month_repository-&gt;findByUser($this-&gt;user); return $this-&gt;render('AcmeBudgetTrackerBundle:Months:months.html.twig', array( 'all_months' =&gt; $all_months, 'form' =&gt; $form-&gt;createView() )); } </code></pre> <p>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!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload