如何在laravel 5中正确扩展视图?

时间:2017-09-09 17:31:21

标签: laravel templates laravel-5 blade

我有一个项目有2个登录表单,一个用于消费者,一个用于创建者 两者都有不同的"登录后"领域

但是视图应该看起来非常相似,所以我想为什么不要DRY-it™ 问题这是我的解决方案,但我确信有一个更优雅的解决方案,如果有,请赐教。

login.blade.php (主模板)

@extends('layout.login', [
    'loginTitle' => 'Consumer',
    'loginAction' => 'login_consumers'
    ])

login_consumer.blade.php

@extends('layout.login', [
    'loginTitle' => 'Creators',
    'loginAction' => 'login_creators' 
    ])

login_creator.blade.php

JSONArray jsonArray = new JSONArray(yourJsonString);
for (int i = 0; i < jsonArray.length(); i++) {
    JSONObject jsonObject = jsonArray.getJSONObject(i);
    int pk = jsonObject.getInt("pk");
    boolean carPhotoStat = jsonObject.getJSONObject("fields").getBoolean("car_photo_stat");

    System.out.printf("%d: %b\n", pk, carPhotoStat);
}

提前致谢

1 个答案:

答案 0 :(得分:0)

包含标题,正文内容页脚的常见布局 是app.blade.php @yields('')指令允许您继承它,并允许您通过@extends('')指令扩展新内容。另外,您可以通过@show@parent blade指令附加到主样式表中,如下所示。

<!-- Stored in resources/views/layouts/app.blade.php -->

<html>
    <head>
        <title>App Name - @yield('title')</title>
        @section('stylesheets')
            <link rel="stylesheet" type="text/css" href="style.css"> <!-- this is master stylesheet -->
        @show
    </head>
    <body>
        <div class="container">
            @yield('content')
        </div>
    </body>
</html>

扩展布局

<!-- Stored in resources/views/login.blade.php -->

@extends('layouts.app')

@section('title', 'Page Title')

@section('stylesheets')
    @parent

    <link rel="stylesheet" type="text/css" href="login.css">
@endsection

@section('content')
    <p>This is my body content.</p>
@endsection

现在,解释你问题的第二部分。上面的案例是关于一个共同的布局,但是这种情况下你试图获得共同的身体内容,所以有组件&amp;那个插槽。将您的共同正文内容分隔为component并将变量作为slot传递。这来自Laravel 5.4。以前,它被称为部分,通过@include('')指令使用。

https://laravel.com/docs/5.5/blade#components-and-slots

相关问题