Symfony2 - Twig - 如何将参数发送到父模板? [英] Symfony2 - Twig - How can I send parameters to the parent template?

查看:18
本文介绍了Symfony2 - Twig - 如何将参数发送到父模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用 Symfony2 和 Twig 模板的 PHP 项目,但我找不到这个问题的解决方案.

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

我有一个管理包,所有模板都从管理库扩展,管理库有一个带菜单的主模板.

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.

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

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

推荐答案

这是一个简单的例子:

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 #}
{% extends 'YourBundle::base.html.twig' %}

{% set menu_selected = 'two' %}

渲染 page2.html.twig 的输出:

Output from rendering page2.html.twig:

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

这篇关于Symfony2 - Twig - 如何将参数发送到父模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆