Laravel 刀片 - 多种布局?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39572797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:28:44  来源:igfitidea点击:

Laravel blade - multiple layouts?

laravellayout

提问by SeKra

I have difficulties getting my head around laravels blade layouts.

我很难理解 laravel 的刀片布局。

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

互联网上的所有示例和文档(例如 laravel 文档:https://laravel.com/docs/5.3/blade或 youtube 上的视频教程)仅使用一个 master.blade.php 作为布局。

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") )
  1. 创建一个新的布局文件(例如 /views/layouts/product.blade.php)并在我的页面中扩展它(在 /views/pages/product.blade.php 中使用 @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.
  1. 仅使用一个包含所有不同类型的布局文件,并将它们实现为每种类型的页面文件中的部分。

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!

非常感谢你的帮助!

回答by idelara

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):

据我所知,关于如何组织特定于 Laravel 的布局文件夹没有任何“最佳实践”,但这里有一个我如何组织项目的示例(并且适用于我所有的 Laravel 应用程序):

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.

另外,我认为将 webapp 的 UI 版本作为父文件夹很重要。有时,当重做 UI 时,人们往往只是将文件保存在相同的目录下,这从长远来看是不好的,因为您最终会在同一文件夹中拥有大量不同版本站点的文件。

Hopefully this helps!

希望这会有所帮助!

Cheers and good luck!

干杯,祝你好运!

回答by Alexey Mezenin

Good practice is to extend some master layout and then use @includeand @eachto include sidebar, footer, header, banner view etc. This works perfectly even for really big projects. Sometimes you want to use @ifoperator for conditional includes:

好的做法是扩展一些主布局,然后使用@include@each包含侧边栏、页脚、页眉、横幅视图等。即使对于非常大的项目,这也非常有效。有时您想将@if运算符用于条件包含:

@if (condition)
    @include('some.view')
@else
    @include('another.view')
@endif