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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化