douya7282
2014-09-30 18:13
浏览 39
已采纳

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

I am studying Laravel 4 for a couple days and I am still confused how I can insert a partial view inside another view or layout. On official documentation of Laravel, they talk about using "sub-view" on controller, using following code:

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

But what I want to do is insert a partial view directly into my "mastaer layout". It's better to explain with codes.

I have this partial view with "messages div" localized in 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

My master template is localized in views/layout.blade.php something like that:

<!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>

How can I insert my messages.blade.php into layout.blade.php? This way, can I send messages for every view that uses layout.blade.php?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongshanxiao7328 2014-09-30 18:15
    已采纳

    You can @include templates from views as well:

    @include('view.name')
    

    In your case, from your layout.blade.php you can just do:

    @include('template.messages')
    

    Documentation

    点赞 打赏 评论

相关推荐 更多相似问题