Possibility to choose a layout based on a condition


#1

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

@section('content')
  <div class="container">
    Hello, world!
  </div>
@endsection

And I did not succeed.

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

Thanks! :smiley:


#2

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.


#3

Hi. Can you give an example?

Thanks.


#4

Bump. :wave:


#5

@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

#6

Okay. Thank you, Virk. :smiley: