Symfony2 - Twig - 如何将参数发送到父模板?

时间:2021-09-03 07:02:10

I am working on a PHP project using Symfony2 with Twig templating, and I can't find a solution for this problem.

我正在使用带有Twig模板的Symfony2进行PHP项目,我无法找到解决此问题的方法。

I have an admin bundle and all the templates extend from admin base which has a master template with a menu.

我有一个管理包,所有模板都从管理员基础扩展而来,管理员基础有一个带有菜单的主模板。

I need to set the current tab of the menu in the base template of the page to selected when the user is on that page.

当用户在该页面上时,我需要将页面基本模板中菜单的当前选项卡设置为选中状态。

Is there any way to pass parameter to the base template through extends?

有没有办法通过扩展将参数传递给基本模板?

2 个解决方案

#1


71  

Here is a simple example:

这是一个简单的例子:

base.html.twig:

base.html.twig:

{# base.html.twig #}
...
<ul>
  <li{% if menu_selected|default('one') == 'one' %} class="selected"{% endif %}>One</li>
  <li{% if menu_selected == 'two' %} class="selected"{% endif %}>Two</li>
  <li{% if menu_selected == 'three' %} class="selected"{% endif %}>Three</li>
</ul>
...

page2.html.twig:

page2.html.twig:

{# page2.html.twig #}
{% extends 'YourBundle::base.html.twig' %}

{% set menu_selected = 'two' %}

Output from rendering page2.html.twig:

渲染page2.html.twig的输出:

<ul>
  <li>One</li>
  <li class="selected">Two</li>
  <li>Three</li>
</ul>

#2


2  

A better way that I just discovered is the basic approach by checking the route for the shortcut route name:

我刚刚发现的一种更好的方法是通过检查快捷方式路由名称的路由来实现基本方法:

<li class="{% if app.request.attributes.get('_route') == 'homepage' %}active{% endif %}">Home</li>

Or another way is to name all your route shortcut names according to the group it belongs to. For example all the routes from your products controller start with "product_...." and then in the template you can do this:

或者另一种方法是根据所属的组命名所有路由快捷方式名称。例如,产品控制器中的所有路径都以“product _....”开头,然后在模板中可以执行以下操作:

<li class="{% if app.request.attributes.get('_route') starts with 'product' %}active{% endif %}">

#1


71  

Here is a simple example:

这是一个简单的例子:

base.html.twig:

base.html.twig:

{# base.html.twig #}
...
<ul>
  <li{% if menu_selected|default('one') == 'one' %} class="selected"{% endif %}>One</li>
  <li{% if menu_selected == 'two' %} class="selected"{% endif %}>Two</li>
  <li{% if menu_selected == 'three' %} class="selected"{% endif %}>Three</li>
</ul>
...

page2.html.twig:

page2.html.twig:

{# page2.html.twig #}
{% extends 'YourBundle::base.html.twig' %}

{% set menu_selected = 'two' %}

Output from rendering page2.html.twig:

渲染page2.html.twig的输出:

<ul>
  <li>One</li>
  <li class="selected">Two</li>
  <li>Three</li>
</ul>

#2


2  

A better way that I just discovered is the basic approach by checking the route for the shortcut route name:

我刚刚发现的一种更好的方法是通过检查快捷方式路由名称的路由来实现基本方法:

<li class="{% if app.request.attributes.get('_route') == 'homepage' %}active{% endif %}">Home</li>

Or another way is to name all your route shortcut names according to the group it belongs to. For example all the routes from your products controller start with "product_...." and then in the template you can do this:

或者另一种方法是根据所属的组命名所有路由快捷方式名称。例如,产品控制器中的所有路径都以“product _....”开头,然后在模板中可以执行以下操作:

<li class="{% if app.request.attributes.get('_route') starts with 'product' %}active{% endif %}">