简体   繁体   中英

Twig template access old variable value in Symfony 3.4

I'm struggling with a problem in Symfony 3.4 related to Twig. Basically, I have two variables and I want to use them as values for javascript in a template. These two variables ( $selected_tipoopera and $selected_year ) can be changed by a form which is managed by the same controller that renders the template.

The controller's code look like this:

    $selected_tipo_opera = "S"; // the default value
    $selected_year = ""; // the default value

    // other stuff / code (form creation and management of form events)

    $form_operas_type_and_year->handleRequest($request);

    if($form_operas_type_and_year->isSubmitted() && $form_operas_type_and_year->isValid()){
        $selected_tipo_opera = $form_operas_type_and_year->getData()["operas_type"];
        $selected_year = $form_operas_type_and_year->getData()["year"];
    }


    // fetch data based on $selected_tipo_opera and $selected_year

    return $this->render('frontend/opere.html.twig', 
        array(
            "opere" => $opere,
            "form_operas_type_and_year" => $form_operas_type_and_year->createView(),
            "selected_tipoopera" => $selected_tipo_opera,
            "selected_year" => $selected_year
        ));

The strange thing is that the controller catches the change of values for the two variables whereas the twig template print always "S" and "" for $selected_tipoopera and $selected_year .

What I'am doing wrong?It is something related with the form?

thanks in advance for any suggestion.

UPDATE

<script type="text/javascript">
    var year = "{{ selected_year }}";
    var stp = "{{ selected_tipoopera }}";
</script>

Here's something that might help. Background: the home page of the site shows a chart created with highcharts , a javascript chart generator. Because the chart needs values from the database I needed to create a Twig template that could be used as javascript.

The keys: a ...js.twig file containing javascript, a controller action to render (not renderView) the template, and a render(controller(...)) in the page(s) where the script is required to insert the script.

So I have:

script.js.twig

$(document).ready(function () {
    var options = {
        chart: {
            type: 'line'
        },
        title: {
            text: 'Distributions, FY{{ chart.fy }} to date'
        },
        xAxis: {
            categories: [{% for category in chart.categories %}{{category|raw}}{%endfor%}]
        },
        yAxis: {
            title: {
                text: 'Distributions'
            }
        },
        series: {{ chart.series|raw }}
    };

    $('#linechart').highcharts(options);
})

And in index.html.twig"

<script type="text/javascript">
    {{ render(controller('AppBundle:Default:script')) }}
</script>

and in ../Controller/Default/script

public function scriptAction(FYChart $fiscalYearChart)
{
    $chart = $fiscalYearChart->getDistsFYToDate();

    return $this->render('Default/script.js.twig', array(
                'chart' => $chart,
    ));
}

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