17

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?

2 Answers 2

42

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.