如何在模板中重用刀片局部 [英] How to reuse a blade partial in a template
问题描述
我希望能够在视图中重复执行多次,每次重复的内容不同.
I would like to be able to repeat a partial a number of times within a view, with different content in each repetition.
partial是一个简单的面板,带有标题和一些内容.每个面板中的内容可能会有所不同,因此我希望能够使用@section('content')
传递数据的方法.
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.
我的设置如下:
panel.blade.php -要重复的部分.
<div class="panel">
<header>
@yield('heading')
</header>
<div class="inner">
@yield('inner')
</div>
</div>
view.blade.php -重复部分视图的视图
view.blade.php - The view in which the partial is repeated
@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
我遇到了与此相同的问题: http://forums.laravel. io/viewtopic.php?id = 3497
I am running into the same issue as this: 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?
推荐答案
对于Laravel 5.4,组件和放大器;插槽可能对您有用.以下解决方案适用于Laravel 4.x,也可能是< = 5.3.
For Laravel 5.4, Components & Slots may be useful for you. The following solution is for Laravel 4.x, and likely <= 5.3 as well.
在我看来,这是@include
语法的愚蠢用例.您节省的HTML重键入次数可以忽略不计,尤其是因为inner
内容是其中唯一可能复杂的部分.请记住,需要完成的解析越多,应用程序的开销也就越大.
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.
此外,我不知道@yield
& @section
功能,所以我不能说以这种方式来工作您的包是多么合适".包含通常利用键=>值对作为包含调用中的参数传递:
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:
@include('panel', ['heading' => 'Welcome', 'inner' => '<p>Some stuff here.</p>'])
这不是打包一堆HTML的最理想的地方,但这是设计的"方式(至少据我所知).
Not the most ideal place to pack a bunch of HTML, but that's the "designed" way (as far as I know, at least).
说...
使用其他控件结构"中提到的@section ... @overwrite
语法模板文档页面的一部分.
Use the @section ... @overwrite
syntax mentioned in the "Other Control Structures" part of the template docs page.
@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
这篇关于如何在模板中重用刀片局部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!