如何使用Laravel 4在默认主布局中包含视图?

时间:2014-09-30 18:13:19

标签: php laravel laravel-4

我正在研究Laravel 4几天,我仍然很困惑如何在另一个视图或布局中插入局部视图。在Laravel的official documentation,他们谈到在控制器上使用“子视图”,使用以下代码:

  

$ view = View :: make('greeting') - > nest('child','child.view',$ data);

但我想做的是将部分视图直接插入我的“mastaer布局”。用代码解释会更好。

我有这个局部视图,其中“messages div”已在 views / template / messages.blade.php 中本地化了:

@section('messages')
    <div id="message">
        @if (isset($errors) && !$errors->isEmpty())
            <!-- Error or fatal messages -->
            <div class="panel panel-danger">
                <div class="panel-heading">{{ Lang::choice('error.message.title', $errors->count()) }}</div>
                <div class="panel-body">
                    <ul class="list-group">
                        @foreach($errors->getMessages() as $errorMessage)
                            <li class="list-group-item">{{ $errorMessage }}</li>
                        @endforeach
                    </ul>
                </div>
            </div>
        @elseif (isset($warnings) && !$warnings->isEmpty())
            <!-- Warning messages -->
            <div class="panel panel-warning">
                <div class="panel-heading">{{ Lang::choice('warn.message.title', $warnings->count()) }}</div>
                <div class="panel-body">
                    <ul class="list-group">
                        @foreach($warnings->getMessages() as $warnMessage)
                            <li class="list-group-item">{{ $warnMessage }}</li>
                        @endforeach
                    </ul>
                </div>
            </div>
        @elseif (isset($successes) && !$successes->isEmpty())
            <!-- Successes messages -->
            <div class="panel panel-success">
                <div class="panel-heading">{{ Lang::choice('success.message.title', $successes->count()) }}</div>
                <div class="panel-body">
                    <ul class="list-group">
                        @foreach($successes->getMessages() as $successMessage)
                            <li class="list-group-item">{{ $successMessage }}</li>
                        @endforeach
                    </ul>
                </div>
            </div>
        @elseif (isset($infos) && !$infos->isEmpty())
            <!-- Informations messages -->
            <div class="panel panel-info">
                <div class="panel-heading">{{ Lang::choice('info.message.title', $infos->count()) }}</div>
                <div class="panel-body">
                    <ul class="list-group">
                        @foreach($infos->getMessages() as $infoMessage)
                            <li class="list-group-item">{{ $infoMessage }}</li>
                        @endforeach
                    </ul>
                </div>
            </div>
        @else
            <!-- Default / no format message -->
            <div class="panel panel-default">
                <div class="panel-heading">{{ Lang::choice('default.message.title', $messages->count()) }}</div>
                <div class="panel-body">
                    <ul class="list-group">
                        @foreach($messages->getMessages() as $message)
                            <li class="list-group-item">{{ $message }}</li>
                        @endforeach
                    </ul>
                </div>
            </div>
        @endif
    </div>
@stop

我的主模板已在 views / layout.blade.php 中进行了本地化:

<!DOCTYPE html>
<html lang="{{ App::getLocale() }}">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        {{ HTML::script('js/bootstrap/bootstrap.min.js') }}
        {{ HTML::script('js/jquery/jquery-2.1.1.min.js') }}
        {{ HTML::style('css/bootstrap/bootstrap.min.css') }}
        {{ HTML::style('css/bootstrap/bootstrap-theme.min.css') }}
        {{ HTML::style('css/custom/custom.css') }}
        <title>Title of the document</title>

    </head>

    <body>
        <div id="generalwrapper">
            <div id="headerwrapper">
                HEADER
            </div>

            <div id="menuwrapper">
                MENU
            </div>

            {{ INSERT MESSAGES VIEW HERE }}

            <div id="content" class="container-fluid">
                @yield('content')
            </div>

            <div id="footerwrapper">
                FOOTER
            </div>
        </div>
    </body>
</html>

如何将messages.blade.php插入layout.blade.php?这样,我可以为每个使用layout.blade.php的视图发送消息吗?

1 个答案:

答案 0 :(得分:1)

您也可以从视图中@include模板:

@include('view.name')

在您的情况下,您可以从layout.blade.php开始:

@include('template.messages')

Documentation