Laravel刀片在延伸范围内延伸

时间:2017-12-16 17:46:10

标签: php laravel blade laravel-blade

我想要扩展2个不同的布局文件。

app.blade.php是主要布局:

<!DOCTYPE html>
<html lang="{{ app()->getLocale() }}">
<head>
    <!-- Meta -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="csrf-token" content="{{ csrf_token() }}">

    <title>@yield('title') | {{ config('app.name') }}</title>

    <!-- Styles -->
    <link rel="stylesheet" href="{{ asset('laraback/css/bootstrap.min.css') }}">
    <link rel="stylesheet" href="{{ asset('laraback/css/fontawesome.min.css') }}">
    <link rel="stylesheet" href="{{ asset('laraback/css/datatables.min.css') }}">
</head>
<body>

@yield('content')

<!-- Scripts -->
<script src="{{ asset('laraback/js/jquery.min.js') }}"></script>
<script src="{{ asset('laraback/js/popper.min.js') }}"></script>
<script src="{{ asset('laraback/js/bootstrap.min.js') }}"></script>
@stack('scripts')

</body>
</html>

form.blade.php是我想要使用的子布局:

<div class="container h-100">
    <div class="row h-100 justify-content-center align-items-center">
        <div class="col-6">
            <div class="card">
                <div class="card-body">
                    @yield('content')
                </div>
            </div>
        </div>
    </div>
</div>

这是我的表单代码:

@section('content')
    <form method="POST" action="{{ route('login') }}" novalidate>
        {{ csrf_field() }}

        <div class="form-group">
            <label for="email">Email</label>
            <input type="email" name="email" id="email" class="form-control">
        </div>

        <div class="form-group">
            <label for="password">Password</label>
            <input type="password" name="password" id="password" class="form-control">
        </div>

        <div class="form-check">
            <label class="form-check-label">
                <input type="checkbox" name="remember" class="form-check-input">
                Remember
            </label>
        </div>

        <button type="submit" class="btn btn-primary">Login</button>
        <a class="btn btn-link" href="{{ route('password.email') }}">Forgot Your Password?</a>
    </form>
@endsection

我知道我可以使用@extends('layouts.app')来获取主要布局中的表单,但是如何才能将form.blade.php扩展为app.blade.php? (我知道这听起来令人困惑)。

这是我想要的结果的一个例子:

<!DOCTYPE html>
<html lang="{{ app()->getLocale() }}">
<head>
    <!-- Meta -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="csrf-token" content="{{ csrf_token() }}">

    <title>@yield('title') | {{ config('app.name') }}</title>

    <!-- Styles -->
    <link rel="stylesheet" href="{{ asset('laraback/css/bootstrap.min.css') }}">
    <link rel="stylesheet" href="{{ asset('laraback/css/fontawesome.min.css') }}">
    <link rel="stylesheet" href="{{ asset('laraback/css/datatables.min.css') }}">
</head>
<body>

<div class="container h-100">
    <div class="row h-100 justify-content-center align-items-center">
        <div class="col-6">
            <div class="card">
                <div class="card-body">
                    <form method="POST" action="{{ route('login') }}" novalidate>
                        {{ csrf_field() }}

                        <div class="form-group">
                            <label for="email">Email</label>
                            <input type="email" name="email" id="email" class="form-control">
                        </div>

                        <div class="form-group">
                            <label for="password">Password</label>
                            <input type="password" name="password" id="password" class="form-control">
                        </div>

                        <div class="form-check">
                            <label class="form-check-label">
                                <input type="checkbox" name="remember" class="form-check-input">
                                Remember
                            </label>
                        </div>

                        <button type="submit" class="btn btn-primary">Login</button>
                        <a class="btn btn-link" href="{{ route('password.email') }}">Forgot Your Password?</a>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Scripts -->
<script src="{{ asset('laraback/js/jquery.min.js') }}"></script>
<script src="{{ asset('laraback/js/popper.min.js') }}"></script>
<script src="{{ asset('laraback/js/bootstrap.min.js') }}"></script>
@stack('scripts')

</body>
</html>

1 个答案:

答案 0 :(得分:1)

在app.blade.php中,您可以像代码一样使用@yield('content')。 app.blade.php很完美。

在你的form.blade.php中,请修改如下。

@extends('layouts.app')
@section('content')
<div class="container h-100">
    <div class="row h-100 justify-content-center align-items-center">
        <div class="col-6">
            <div class="card">
                <div class="card-body">
                    @include('form_content')
                </div>
            </div>
        </div>
    </div>
</div>

@endsection

你需要在没有@section和@endsection的情况下使用你的表单内容制作form_content.blade.php文件。

<form method="POST" action="{{ route('login') }}" novalidate>
    {{ csrf_field() }}

    <div class="form-group">
        <label for="email">Email</label>
        <input type="email" name="email" id="email" class="form-control">
    </div>

    <div class="form-group">
        <label for="password">Password</label>
        <input type="password" name="password" id="password" class="form-control">
    </div>

    <div class="form-check">
        <label class="form-check-label">
            <input type="checkbox" name="remember" class="form-check-input">
            Remember
        </label>
    </div>

    <button type="submit" class="btn btn-primary">Login</button>
    <a class="btn btn-link" href="{{ route('password.email') }}">Forgot Your Password?</a>
</form>

然后您可以根据需要查看结果页面。

您可以使用@if对@include子句进行分类。 这意味着你可以为每个@include创建一个条件子句。 因此,如果条件适合,您可以包含@include部分之一。