在Blade中包含子视图

时间:2015-07-14 15:19:16

标签: laravel laravel-5 blade

我试图在'home'视图中包含两个子视图('login'和'register'),如下所示:

@extends('master')

@section('content')

    @include('auth.login')
        <hr>
    @include('auth.register')

@endsection

“登录”和“注册”视图:

//register.blade.php

@extends('master')

@section('content')

{!! Form::open() !!}

    <div class="form-group">
        {!! Form::label('email', 'Email Address') !!}
        {!! Form::email('email', null, 
            ['class' => 'form-control', 
             'placeholder' => 'Email Address', 
             'required' => true]) !!}
    </div>

    <div class="form-group">
        {!! Form::label('password', 'Password') !!}
        {!! Form::password('password', 
            ['class' => 'form-control',
             'placeholder' => 'Password', 
             'required' => true]) !!}
    </div>

    <div class="checkbox">
        <label>
            {!! Form::checkbox('remember', null, []) !!}
            Remeber Me
        </label>
    </div>

    {!! Form::submit('Login', ['class' => 'btn btn btn-primary']) !!}

{!! Form::close() !!}

@endsection
//login.blade.php
@extends('master')

@section('content')

{!! Form::open() !!}

    <div class="form-group">
        {!! Form::label('email', 'Email Address') !!}
        {!! Form::email('email', null, 
            ['class' => 'form-control', 
             'placeholder' => 'Email Address', 
             'required' => true]) !!}
    </div>

    <div class="form-group">
        {!! Form::label('password', 'Password') !!}
        {!! Form::password('password', 
            ['class' => 'form-control', 
             'placeholder' => 'Password', 
             'required' => true]) !!}
    </div>

    <div class="checkbox">
        <label>
            {!! Form::checkbox('remember', null, []) !!}
            Remeber Me
        </label>
    </div>

    {!! Form::submit('Login', ['class' => 'btn btn btn-primary']) !!}

{!! Form::close() !!}

@endsection

我尝试从子视图中删除主扩展,但它不起作用。只渲染一个子视图。我无法弄清楚,为什么会发生这种情况?

1 个答案:

答案 0 :(得分:0)

<强> main.blade.php

@extends('master')
@section('content')

    @include('auth.login')
        <hr>
    @include('auth.register')

@endsection

对于子视图,您必须删除@extends('master')。那么你有两个我能想到的选择。

您可以添加@parent指令。

<强> register.blade.php

@section('content')
{!! Form::open() !!}

...

    {!! Form::submit('Login', ['class' => 'btn btn btn-primary']) !!}

{!! Form::close() !!}
@parent
@endsection

<强> login.blade.php

@section('content')

{!! Form::open() !!}

...

{!! Form::close() !!}
@parent
@endsection

或删除部分。

<强> register.blade.php

{!! Form::open() !!}

...

    {!! Form::submit('Login', ['class' => 'btn btn btn-primary']) !!}

{!! Form::close() !!}

<强> login.blade.php

{!! Form::open() !!}

...

{!! Form::close() !!}

请参阅此similar question

<小时/> 每条评论更新

如果您需要单独访问这些页面,您可以将表单代码提取为部分内容并将其包含在需要的位置。

<强> main.blade.php

@extends('master')
@section('content')

    @include('auth.partials.login')
        <hr>
    @include('auth.partials.register')

@endsection

设置独立页面。

<强> AUTH / register.blade.php

@section('content')

    @include('auth.partials.register')

@endsection

<强> AUTH / login.blade.php

@section('content')

    @include('auth.partials.login')

@endsection

设置你的部分

<强> AUTH /分音/ register.blade.php

{!! Form::open() !!}

...

{!! Form::submit('Login', ['class' => 'btn btn btn-primary']) !!}

{!! Form::close() !!}

<强> AUTH /分音/ login.blade.php

{!! Form::open() !!}

...

{!! Form::close() !!}