Possibility to choose a layout based on a condition


Hi guys,

I’ve read in the documentation that the @layout tag should be placed on the first line of all views.

But I tried to do something like this:

@loggedIn @layout('blank') @else @layout('master') @endloggedIn

  <div class="container">
    Hello, world!

And I did not succeed.

My question is: is there a way to choose a layout based on a condition?

Thanks! :smiley:


I’d do layout switching on the controller/middleware side. Although I don’t know how to do this, but maybe @virk has a tip for that.


Hi. Can you give an example?



Bump. :wave:


@lffg Nope it’s possible to change the layout of the page at runtime.

Layouts needs to be knows at compile time and with dynamic layouts, we cannot compile them ahead of time. So it’s not possible.

What you need in this case is a component. Create multiple components instead of layouts and dynamically switch between them.

The differences between component and layout are:

  1. Components use slots over sections
  2. Components state is isolated, so you will have to pass values as props from your parent template


Okay. Thank you, Virk. :smiley: