laravel 如何在模板中重用刀片部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18840775/
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
How to reuse a blade partial in a template
提问by tahlo
I would like to be able to repeat a partial a number of times within a view, with different content in each repetition.
我希望能够在一个视图中多次重复部分内容,每次重复都有不同的内容。
The partial is a simple panel, with a heading, and some content. The content within each panel can vary in complexity, so I would like to be able to use the @section('content')
method of passing data.
部分是一个简单的面板,有一个标题和一些内容。每个面板中的内容的复杂程度可能各不相同,因此我希望能够使用@section('content')
传递数据的方法。
The set up I have is as follows:
我的设置如下:
panel.blade.php- The partial to be repeated.
panel.blade.php- 要重复的部分。
<div class="panel">
<header>
@yield('heading')
</header>
<div class="inner">
@yield('inner')
</div>
</div>
view.blade.php- The view in which the partial is repeated
view.blade.php- 重复部分的视图
@extends('default')
@section('content')
{{-- First Panel --}}
@section('heading')
Welcome, {{ $user->name }}
@stop
@section('inner')
<p>Welcome to the site.</p>
@stop
@include('panel')
{{-- Second Panel --}}
@section('heading')
Your Friends
@stop
@section('inner')
<ul>
@foreach($user->friends as $friend)
<li>{{ $friend->name }}</li>
@endforeach
</ul>
@stop
@include('panel')
@stop
I am running into the same issue as this: http://forums.laravel.io/viewtopic.php?id=3497
我遇到了与此相同的问题:http: //forums.laravel.io/viewtopic.php?id=3497
The first panel is displayed as intended, but the second is simply a repeat of the first.
第一个面板按预期显示,但第二个面板只是第一个面板的重复。
How can I correct this? If this is a poor method of getting this done, what would be a better way?
我该如何纠正?如果这是完成这项工作的糟糕方法,那么更好的方法是什么?
回答by Aken Roberts
For Laravel 5.4, Components & Slotsmay be useful for you. The following solution is for Laravel 4.x, and likely <= 5.3 as well.
对于 Laravel 5.4,Components & Slots可能对你有用。以下解决方案适用于 Laravel 4.x,也可能 <= 5.3。
In my opinion, this is a silly use case for the @include
syntax. The amount of HTML retyping you're saving is negligible, especially since the only potentially-complicated part of it is the inner
content. Keep in mind, the more parsing that needs to be done, the more overhead your application has, also.
在我看来,这是@include
语法的一个愚蠢的用例。您节省的 HTML 重新输入量可以忽略不计,尤其是因为它唯一可能复杂的部分是inner
内容。请记住,需要进行的解析越多,应用程序的开销也越大。
Also, I don't know the inner workings of the @yield
& @section
features, so I can't say how "proper" it is to work your includes this way. Includes typically utilize a key => value pair passed as a parameter in the include call:
另外,我不知道@yield
&@section
功能的内部工作原理,所以我不能说以这种方式工作你的包含是多么“正确”。Includes 通常使用 key => value 对作为参数传递给 include 调用:
@include('panel', ['heading' => 'Welcome', 'inner' => '<p>Some stuff here.</p>'])
Not the most ideal place to pack a bunch of HTML, but that's the "designed" way (as far as I know, at least).
不是打包一堆 HTML 的最理想的地方,但这是“设计”的方式(至少据我所知)。
That said...
那说...
Use the @section ... @overwrite
syntax mentioned in the "Other Control Structures"part of the template docs page.
使用模板文档页面@section ... @overwrite
的“其他控制结构”部分中提到的语法。
@extends('default')
@section('content')
{{-- First Panel --}}
@section('heading')
Welcome, {{ $user->name }}
@overwrite
@section('inner')
<p>Welcome to the site.</p>
@overwrite
@include('panel')
{{-- Second Panel --}}
@section('heading')
Your Friends
@overwrite
@section('inner')
<ul>
@foreach($user->friends as $friend)
<li>{{ $friend->name }}</li>
@endforeach
</ul>
@overwrite
@include('panel')
@stop