0

有没有办法只包含 Laravel 刀片视图的部分?


我有一个基本视图,通常在此视图中包含内容。有时我需要更多的自由并想要一个更简单的基础,所以我将$special标志设置为 true。现在我有一个既可以作为“特殊”视图又可以作为正常视图的视图。有没有一种巧妙的方法来干燥这个?

base.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>@yield("title", "placeholder") - website</title>
</head>
<body>
    @if (isset($special) && $special)
        @yield("content")
    @else
        <header>
            website
        </header>
        <main>
            @yield("content")
        </main>
        <footer>&copy; 2099</footer>
    @endif
</body>
</html>

article.blade.php

@extends("base")

@section("title", "10 ways! You won't follow the last!")

@section("content")
So much content.
@endsection

other.blade.php

@extends("base", ["special" => true])

@section("title", "Welcome")

@section("content")
<div id="start">
    Other stuff
</div>
<div id="wooo">
    <main>
        @include("article") ← does not work
    </main>
    <footer>&copy; 2099</footer>
</div>
@endsection
4

2 回答 2

0

我最终制作了一个仅包含该部分的新刀片文件。然后两个页面都包含该刀片模板。

article.blade.php

@extends("base")

@section("title", "10 ways! You won't follow the last!")

@section("content")
@include("common")
@endsection

other.blade.php

@extends("base", ["special" => true])

@section("title", "Welcome")

@section("content")
<div id="start">
    Other stuff
</div>
<div id="wooo">
    <main>
        @include("common")
    </main>
    <footer>&copy; 2099</footer>
</div>
@endsection

common.blade.php

So much content.
于 2016-01-04T19:37:28.450 回答
0

我通常这样做:

我创建了一个base.blade.php包含布局的核心内容的内容。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>

    @yield('body-content')

</body>
</html>

然后我创建了其他扩展基本文件的模板。

例如:
template.blade.php

@extends('base')

@section('body-content')

    <header>
        website
    </header>
    <main>
        @yield("content")
    </main>
    <footer>&copy; 2099</footer>

@endsection

someOtherTemplate.blade.php

@extends('base')

@section('body-content')
    <main>
        @yield("content")
    </main>
@endsection

现在只需扩展您需要的任何模板。

于 2016-01-04T21:44:36.187 回答