Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel blade - multiple layouts?

Tags:

layout

laravel

I have difficulties getting my head around laravels blade layouts.

All the examples and documentations around the internet (e.g. laravel documentation: https://laravel.com/docs/5.3/blade or video tutorials on youtube) only use one master.blade.php as layout.

Is there a best practice for more complex projects?

The following content types are included in my project:

  • product
  • category
  • blog
  • taxonomy
  • home
  • administrative
  • login/auth

All of these content types have different layouts:

  • different/no sidebar
  • sidebar on left/right
  • different header
  • no/banner before content
  • different/no menu
  • different/no breadcrumb

So I don't know in what situation ...

  1. create a new layout file (e.g. /views/layouts/product.blade.php) and extend it in my page (in /views/pages/product.blade.php with @extends("layouts.product") )

... or ...

  1. use only one layout file that contains all different types and implement them as sections in the page file of each type.

This one drives me crazy right now and I couldn't find anything valuable like a best practice wether to use layouts or not.

Thank you so much for your help!

like image 794
SeKra Avatar asked Sep 19 '16 12:09

SeKra


1 Answers

A layout structure can easily become a mess, that is why it is heavily recommended to keep layouts and partials organized in an intuitive folder structure. By doing so, you will ensure that in the future, when your app grows, it will stay clean and organized. It also depends on which kind of project you are working on. Believe it or not, sometimes the folder structure varies from project to project.

As far as I know, there are not any "best practices" on how to organize a layout folder specific to Laravel, but here is an example of how I organize my projects (and has worked for all my Laravel apps out there):

views/
├── v1/
│   ├── master
|   |   ├── master-public.blade.php
|   |   ├── master-admin.blade.php
|   |   ├── master-user.blade.php
|   ├── components
|   │   ├── navigation
|   |   |   ├── public.blade.php
|   |   |   ├── admin.blade.php
|   |   |   ├── user.blade.php
|   |   ├── headers
|   |   ├── footers
|   ├── views
|   |   ├── home
|   |   ├── chat
|   |   ├── order
|   |   ├── reports
|   ├── partials
|   |   ├── ads.blade.php
|   |   ├── sidebar.blade.php
|   ├── public
|   |   ├── registration.blade.php
|   |   ├── login.blade.php
├── v2/
└── v2.2/

The most important thing to mention here is that inside my views directory I create a folder per each route I end up having in my app.

Also, I believe that it is important to have as parent folders the version of the UI of the webapp. Sometimes, when redoing the UI one tends to just save the files under the same directories, which is not good long-term since you will end up having a sea of files for different versions of your site in the same folder.

Hopefully this helps!

Cheers and good luck!

like image 114
idelara Avatar answered Oct 31 '22 05:10

idelara