Laravel Blade:@extend 一个(已经扩展的)子刀片?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33344580/
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
Laravel Blade: @extend a (already extended) child blade?
提问by Fixspec
Anyone know if it's possible to extend a child blade?
有谁知道是否可以延长儿童刀片?
My app has one common layout template, and then each page @extends from that. Each page may pull in a series of @includes for other chunks of HTML as required (eg modals).
我的应用程序有一个通用布局模板,然后每个页面@extends。每个页面都可以根据需要为其他 HTML 块(例如模态)引入一系列 @include。
@extends('common.layout')
@section('content')
... Some other content ...
@include('modals.modal-1')
@include('modals.modal-2')
@endsection
All of the modals have a lot of common boilerplate code (Bootstrap), so what I'd like to do is define a master model template, have all modals @extend from that, and then @include those in my pages as required. So /modals/modal-1.blade.php would look something like:
所有的模态都有很多通用的样板代码(Bootstrap),所以我想要做的是定义一个主模型模板,让所有模态从那里@extend,然后根据需要在我的页面中@include。所以 /modals/modal-1.blade.php 看起来像:
@extends('common.modals')
@section('modal-id', 'modal-1')
@section('modal-title','Modal title')
@section('modal-body')
... Some HTML / Blade here ...
@endsection
Every time I try though, the generated HTML is corrupted. In the above case, modal-1 would be shown twice because it appeared first, and modal-2 would not be present at all. Flip the order and modal-2 will appear twice.
每次我尝试时,生成的 HTML 都会损坏。在上述情况下,modal-1 将显示两次,因为它首先出现,而 modal-2 根本不存在。翻转顺序,modal-2 会出现两次。
I guess I could simply place my modal body in a variable and use it in the @include statement @include('modal.blade', ['body' => '<div>...</div>'])
, but using @extends feels more correct.
我想我可以简单地将我的模态体放在一个变量中并在 @include 语句中使用它@include('modal.blade', ['body' => '<div>...</div>'])
,但使用 @extends 感觉更正确。
Any ideas?
有任何想法吗?
回答by Andy Noelker
You can absolutely extend a Blade view from a view that has already been extended. However, you are mixing template inheritance with view inclusion and that is causing your weird results.
您绝对可以从已经扩展的视图中扩展 Blade 视图。但是,您将模板继承与视图包含混合在一起,这导致了奇怪的结果。
When inheriting templates with the @extend
directive, you must always return the lowest child on the chain that you want. Let's say you have 3 generations of templates:
使用@extend
指令继承模板时,您必须始终返回所需链中最低的子代。假设您有 3 代模板:
//grandparent.blade.php
<html>
<head>
<title>My App</title>
</head>
<body>
@yield('parent-content')
</body>
</html>
//parent.blade.php
@extends('grandparent')
@section('parent-content')
<div class="container">
@yield('child-content')
</div>
@endsection
//child.blade.php
@extends('parent')
@section('child-content')
<div class="page">
//stuff
</div>
@endsection
In this instance, you would return the child view and it would also contain the two generations of templates above it. But you could neverreturn parent.blade.php
and expect it to also return that child view. There might be 100 child views that extend the parent, so there would be no way of knowing which one.
在这种情况下,您将返回子视图,它还将包含其上方的两代模板。但是您永远无法返回parent.blade.php
并期望它也返回该子视图。可能有 100 个子视图扩展了父视图,因此无法知道是哪一个。
Think of the @include
directive as just a way to break up the HTML in your view nicely into smaller bits. Often you will use it for reusable pieces of code that you want to reference in multiple views. It is not the same thing as template inheritance though. Also remember that an included view will get all of the same data as its parent view (and you can even pass it more).
将该@include
指令视为将视图中的 HTML 很好地分解为更小的部分的一种方式。通常,您会将它用于要在多个视图中引用的可重用代码段。不过,它与模板继承不同。还要记住,包含的视图将获得与其父视图相同的所有数据(您甚至可以传递更多数据)。
In your case, you have to decide what constitutes the base root of the page. Is the core of page modal-1
? If so, you need to return modal-1
from your controller as your child view and extend it up the chain. In that case, leave the file exactly like you have it in your post. Its parent view (common.modals
) would need to be changed to this:
在您的情况下,您必须决定什么构成页面的基本根。是页面的核心modal-1
吗?如果是这样,您需要modal-1
从您的控制器返回作为您的子视图并将其扩展到链上。在这种情况下,请按照您在帖子中的原样保留文件。其父视图 ( common.modals
) 需要更改为:
@extends('common.layout')
@section('content')
... Some other content ...
@yield('modal-id')
@yield('modals-title')
@yield('modal-body')
@include('modals.modal-2')
@endsection
Obviously you would put each of those yield statements where it makes sense on the page.
显然,您会将每个 yield 语句放在页面上有意义的地方。
However, if modal-1
is notthe core of the page, but just something extra that you want to include (like a widget), then you should include
it like you are doing in the parent view. In that case, you will need to remove @extends
directive from it and don't bother to wrap the main HTML in any section. It will just be passed to the view as-is. If you include sections in an included template, those sections must be yielded to in the view that includes it. So your modal-1
template would wind up looking like this:
但是,如果modal-1
是没有页面的核心,但只是一些额外的东西,你要包括(如部件),那么你就应该include
像你在父视图做的事情。在这种情况下,您需要从中删除@extends
指令,并且不必费心将主 HTML 包装在任何部分中。它只会按原样传递给视图。如果在包含的模板中包含部分,则必须在包含它的视图中让出这些部分。所以你的modal-1
模板最终看起来像这样:
<div>
<p>HTML goes here. No need to extend anything or wrap in a section.
You can still include {{$data}} though.
</p>
</div>
@section('script')
Including this section means that the view that is including this template
already contains a @yield('script') directive.
By including the @parent directive, this section will append that one.
@parent
@endsection
回答by MisterP
I believe the proper way to include templates that already extends some others templates is to use @overwrite
in place of @endsection
in your included template:
我相信包含已经扩展了一些其他模板的模板的正确方法是在您包含的模板中使用@overwrite
代替@endsection
:
@extends('common.modals')
@section('modal-id', 'modal-1')
@overwrite
@section('modal-title','Modal title')
@overwrite
@section('modal-body')
... Some HTML / Blade here ...
@overwrite