简体   繁体   English

Twig模板访问Symfony 3.4中的旧变量值

[英]Twig template access old variable value in Symfony 3.4

I'm struggling with a problem in Symfony 3.4 related to Twig. 我在Symfony 3.4中与Twig有关的问题中苦苦挣扎。 Basically, I have two variables and I want to use them as values for javascript in a template. 基本上,我有两个变量,我想将它们用作模板中javascript的值。 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. 这两个变量( $selected_tipoopera$selected_year )可以通过一种形式进行更改,该形式由呈现模板的同一控制器管理。

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 . 奇怪的是,控制器捕获了两个变量的值变化,而树枝模板始终为$selected_tipoopera$selected_year打印“ S”和“”。

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. 背景:该网站的首页显示了一个由highcharts创建的图表,该图表是javascript图表生成器。 Because the chart needs values from the database I needed to create a Twig template that could be used as javascript. 因为图表需要数据库中的值,所以我需要创建一个Twig模板以用作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. 关键:一个包含JavaScript的...js.twig文件,一个用于渲染(而非renderView)模板的控制器动作以及一个需要插入脚本的页面中的render(controller(...)) 。剧本。

So I have: 所以我有:

script.js.twig 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" 并且在index.html.twig中”

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

and in ../Controller/Default/script 并在../Controller/Default/script中

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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