如何在模板中重用刀片部分 [英] How to reuse a blade partial in a template

查看:23
本文介绍了如何在模板中重用刀片部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在一个视图中多次重复部分内容,每次重复都有不同的内容.

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 - 部分重复的视图

@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 语法rel="noreferrer">其他控制结构" 模板文档页面的一部分.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆