symfony2 - twig - 如何从树枝模板内部渲染树枝模板

时间:2021-11-13 07:13:23

I have a xxx.html.twig file which shows a page, but when I want to refresh the page with different data and just update it with new data, I have a select and a submit button for it. The thing is that I don't know how do I call an action in the controller which I pass parameters to from my twig and call for new data and then I render the same twig template again with new parameters.

我有一个显示页面的xxx.html.twig文件,但是当我想用不同的数据刷新页面并用新数据更新它时,我有一个选择和提交按钮。问题是我不知道如何在控制器中调用一个动作,我从我的树枝传递参数并调用新数据,然后我用新参数再次渲染相同的树枝模板。

How do I do so?

我该怎么办?

3 个解决方案

#1


31  

Symfony 2.1:

{% render 'YourBundle:YourController:yourAction' with {'var': value} %}

Symfony 2.6+:

{{ render(controller('YourBundle:YourController:yourAction', {'var': value})) }}

And, of course, read the documentation.

当然,请阅读文档。

#2


40  

Here are a few different ways:

以下是几种不同的方式:

{{ render(app.request.baseUrl ~ '/helper/test', {"hostid2": hostid } ) }}

or

要么

{% include 'MyCoreBundle:Helper:test.html.twig' with {"hostid2": hostid } only %}

or

要么

{% render controller("MyCoreBundle:Helper:test", {'hostid2': hostid}) %}

#3


0  

I think some parts are depricated here. To make the include work in latest Symfony 3.1.10, I solved it like this:

我认为有些部分在这里被贬低了。为了使include工作在最新的Symfony 3.1.10中,我解决了这个问题:

{% extends 'base.html.twig' %}
{% block body %}
    {{ include('AppBundle:Default:inner_content.html.twig') }}
{% endblock %}

Note: include() with parentheses. Then all the variables are included from the parent template. If you like to restrict some variables in the child template, you use with ... only (look over)

注意:带括号的include()。然后,所有变量都包含在父模板中。如果您想限制子模板中的某些变量,则只使用...(查看)

#1


31  

Symfony 2.1:

{% render 'YourBundle:YourController:yourAction' with {'var': value} %}

Symfony 2.6+:

{{ render(controller('YourBundle:YourController:yourAction', {'var': value})) }}

And, of course, read the documentation.

当然,请阅读文档。

#2


40  

Here are a few different ways:

以下是几种不同的方式:

{{ render(app.request.baseUrl ~ '/helper/test', {"hostid2": hostid } ) }}

or

要么

{% include 'MyCoreBundle:Helper:test.html.twig' with {"hostid2": hostid } only %}

or

要么

{% render controller("MyCoreBundle:Helper:test", {'hostid2': hostid}) %}

#3


0  

I think some parts are depricated here. To make the include work in latest Symfony 3.1.10, I solved it like this:

我认为有些部分在这里被贬低了。为了使include工作在最新的Symfony 3.1.10中,我解决了这个问题:

{% extends 'base.html.twig' %}
{% block body %}
    {{ include('AppBundle:Default:inner_content.html.twig') }}
{% endblock %}

Note: include() with parentheses. Then all the variables are included from the parent template. If you like to restrict some variables in the child template, you use with ... only (look over)

注意:带括号的include()。然后,所有变量都包含在父模板中。如果您想限制子模板中的某些变量,则只使用...(查看)