How to set different template layout for different modules in Symfony

限于喜欢 提交于 2019-12-03 07:10:12

问题


How to set different template layouts for different modules in Symfony?

I have a banking application that consists of a login screen, and a member section. So when a user goes to my site he will be presented with a login screen. After login in he will be redirected to the member section that he can do his whatever banking needs.

So, how to set different layouts for the login screen and the pages inside the member section? Symfony seems to use frontend/templates/layout.php as the template for ALL pages. Is it possible to define different layouts?


回答1:


Yes, you can define separate layouts per view (or disable the layout altogether). To do this, you must create (or edit, if you already have it) the view.yml file in the /config directory of your module. You can define the layout to be used for all views of the module or for each view separately. For example:

#in /apps/my_app/modules/my_module/config/view.yml

#this will apply custom_layout to all views of the module
all:
  layout: custom_layout

#this will apply login_layout to the loginSuccess view
loginSuccess:
  layout: login_layout

#disable layout for this view
homeSuccess:
  has_layout: false

In all cases, the layout is the file in your app's /templates directory (with .php appended). If you do not define any layout directives in the module's view configuration file, the default layout will be used.




回答2:


Or if you are inside actions, you can use simply:

$this->setLayout('name_file_layout_without_extension');

And after you can work on template that you are using inside the module.



来源:https://stackoverflow.com/questions/610133/how-to-set-different-template-layout-for-different-modules-in-symfony

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!