在模板中使用symfony的动态菜单 [英] Dynamic menu with symfony in template

查看:131
本文介绍了在模板中使用symfony的动态菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个symfony网站,我有自己的代码来动态地创建一个包含子项目的菜单。现在我在base.html.twig文件(主布局)中打印一些twig代码。问题是当我需要通过菜单的项目传递数组。我实际上将它传递给单个控制器的render方法,但是当我切换页面时,我感到非常谦虚,我得到一个不存在的变量。如何在单个位置放置我的PHP代码来构建菜单数组,并在每次呈现页面时执行它?

解决方案

p>将菜单创建逻辑放在新的控制器中,并使用twig的 {%render%} 将其包含在您的页面中。请参阅嵌入控制器


I'm building a symfony website and I've my own code to build dynamically a menu with subitems. Right now I print it with a bit of twig code inside the base.html.twig file (master layout). The problem is when I need to pass the array with the items of the menu. I'm actually passing it to the render method of a single controller but obvsiously when I switch page I get a non-existent variable. How can I have a single location to put my php code to build the menu array and execute it every time a page is rendered?

解决方案

Put your menu-creating logic in a new controller, and use twig's {% render %} to include it in your page. See embedding controllers.

这篇关于在模板中使用symfony的动态菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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