是否有可能在Twig模板中获取一个不在查询中的参数而不必将其传递给控制器?
这个想法是从像这样的路由中访问section
值(以避免在模板菜单中添加“活动”CSS类的长Twig条件):
ibcpg_admin_dashboard:
path: /
defaults: { _controller: IBCPGAdministrationBundle:Main:dashboard, section: dashboard }
ibcpg_admin_courses:
path: /courses
defaults: { _controller: IBCPGAdministrationBundle:Course:list, section: courses }
ibcpg_admin_course_add:
path: /course/add
defaults: { _controller: IBCPGAdministrationBundle:Course:add, section: courses }
ibcpg_admin_course_edit:
path: /course/edit/{id}
defaults: { _controller: IBCPGAdministrationBundle:Course:edit, section: courses }
requirements:
id: \d+
ibcpg_admin_course_delete:
path: /course/delete/{id}
defaults: { _controller: IBCPGAdministrationBundle:Course:delete, section: courses }
requirements:
id: \d+
ibcpg_admin_course_chapters:
path: /course-chapters
defaults: { _controller: IBCPGAdministrationBundle:CourseChapter:listAndAdd, section: course_chapters }
答案 0 :(得分:0)
我发现直接在defaults
表(路由中)中定义的所有参数都填充了ParameterBag的属性部分,因此您可以在Twig模板中以这样的方式访问它们:
{{ app.request.attributes.get('my_parameter') }}
对于我给出的示例, my_parameter
将由section
替换。