'Having multiple layouts in laravel
In my site I have seven different HTML Layouts. The reason i ended up having so many is because some pages had different headers/footers and some pages did not pass data from the back end which was required for the layout to work. I've ended up with several layouts. This is a pretty big inconvenience. Is there any way around this?
Solution 1:[1]
another big hidden gem of blade I found was the @stack
and @push
functions.
You can use @push
to add sections to a certain stack
In your template
@stack('scripts')
And then in your views
@push('script')
<!-- Here goes your normal HTML -->
@endpush
Solution 2:[2]
For the layouts that require data, I think you could make it optional in the template and validate the data requirement in the controller instead of the template
Solution 3:[3]
I think i have found a better way around to achieve ( different headers and footers for different pages in laravel ).
views/
??? frontend/
? ??? index.blade.php
| |
? ??? layouts/
| | ??? app.blade.php
| | |
| ??? includes/
| | ??? nav.blade.php
| | |
| | ??? headers/
| | | ??? main-header.blade.php
| | | ??? another-header.blade.php
| | ??? footers/
| | | ??? main-footer.blade.php
| | | ??? another-footer.blade.php
Now in app.blade.php we use the @Yield('')
<body id="body">
@include('includes.partials.read-only')
@yield('header')
<div id="app">
<div class="container">
@include('includes.partials.messages')
@yield('content')
</div><!-- container -->
</div><!-- #app -->
@yield('footer')
And in index.blade.php i want to add the footer another-footer.blade.php
@section('footer')
@include('frontend.includes.footers.another-footer.blade.php')
@endsection
In another pages if i want to use another footer then we can just change the included file inside the footer section.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Jonas Lindenskov Nielsen |
Solution 2 | rebaz sabir |
Solution 3 | Martijn Pieters |