doushi4956 2014-08-12 15:52
浏览 63
已采纳

正确使用Blade(Laravel),在其自己的文件中有标题的问题

I thought I would split the header from my blade templates and have the header and footer included separately. It worked to put my header.blade.php in layouts/partials/, and then in the next template, it extends layouts.partials.header. It works, but the stylesheets and scripts are loaded after the content. How should it be organized in a way that runs fast and in the correct order?

header.blade.php

@section('header')
<!DOCTYPE html>
<html>
<head>
<title>
@section('title')
@show
</title>  
<script type="text/javascript" src="{{ asset('bower/jquery/dist/jquery.min.js') }}"></script>
<link href="{{ asset('bower/bootstrap/dist/css/bootstrap.min.css') }}" rel="stylesheet">
<script type="text/javascript" src="{{ asset('bower/bootstrap/dist/js/bootstrap.min.js') }}"></script>
<script type="text/javascript" src="{{ asset('bower/ckeditor/ckeditor.js') }}"></script>
<link href="{{ asset('css/default.css') }}" rel="stylesheet"> 
</head>
<body>
@show

@section('footer')
    @section('scripts')
    @show
</body>
</html>
@show

master.blade.php

@extends('layouts.partials.header')
@yield('header')

    <div class="container">

        @section('topNav')
            <div class="row center-block text-center indexWrapper">
                <div class="indexNav">
                    <ul class="text-right">
                        <li><a href="{{URL::to('people')}}">People</a></li>
                        <li><a href="{{URL::to('bulletin')}}">Bulletin</a></li>
                        <li><a href="{{URL::to('current')}}">Current</a></li>
                        <li><a href="{{URL::to('finished')}}">Finished</a></li>
                    </ul>
                </div>
                <div class="indexHeading">
                    <h1 class="indexH1">
                        @section('navTitle')
                        @show
                    </h1>
                </div>
                <div class="clearfix"></div>
            </div>
        @show

        @yield('content')

        <div class="center-block login">
            @yield('login')
        </div>

    </div>

    @section('scripts')
    @show

</body>
</html>

home.blade.php

@extends('layouts.master')

    @section('title')
    @parent
        ::Home
    @stop

    @section('navTitle')
    @parent
        Mumble
    @stop

    @section('login')

            @if (Auth::check()) 
                <div class="col-md-12 panel panel-default">
                    <div class="panel-body text-center">
                        <h4>Welcome back  <em>{{ Auth::user()->name }}</em></h4>
                    </div>
                </div>

                <div class="text-center">
                    <a href="logout" class="btn btn-warning">Logout</a>
                </div>
            @else

                @if($error) 
                    <div class="alert alert-danger">
                        {{ $error }}
                    </div>
                @endif

                @if($errors->first('email'))
                    <div class="alert alert-warning">
                        {{ $errors->first('email') }}
                    </div>
                @endif
                @if($errors->first('password'))
                    <div class="alert alert-warning">
                        {{ $errors->first('password') }}
                    </div>
                @endif
                {{ Form::open(array('url' => '')) }}
                    <div class="form-group">
                        {{Form::label('email', 'Email')}}
                        {{Form::text('email', Input::old('email'),array('class'=>'form-control','placeholder'=>'enter email'))}}
                    </div>
                    <div class="form-group">
                        {{Form::label('password', 'Password')}}
                        {{Form::password('password',array('class'=>'form-control','placeholder'=>'enter password'))}}
                    </div>
                    <div class="form-group">

                        {{ Form::checkbox('remember','remember') }}  
                        <span style="margin-left:5px;">Remember Me</span>
                    </div>
                    <div class="text-center">
                        {{ Form::submit('Login',array('class'=>'btn btn-default')) }}
                    </div>
                {{ Form::close() }}
            @endif

    @stop

    @section('scripts')
        <script type="text/javascript">
            $(document).ready(function(){
                //$('.indexWrapper').addClass('homeCenter');
                //$('.indexWrapper').css( 'margin-top', '25%' );
            });
        </script>
    @stop

How modular should things be in blade? Am I breaking it into too many pieces? The scripts run slow when they are in the "footer" (defined in the header partial, I guess I should rename that), but I just want to know if there is a way to do this properly.

  • 写回答

1条回答 默认 最新

  • dongquepao8653 2014-08-12 17:45
    关注

    For my projects, i usually do something like this which works well. The amount of granularity really depends on your own requirements.

    Instead of using @extends, etc.. set your views as properties of your master layout so they are rendered in your controller.

    master.blade.php

    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Sitename | @yield('title')</title>
    
        {{ stylesheet() }}
    
    </head>
    <body class="{{ $authClass }}{{ isset($bodyClass) ? $bodyClass : ''  }}" role="document">
    
        {{ $mainNav }}
    
        <section id="content">
    
            <section id="main">
                @yield('content')
            </section>
    
        </section>
    
        <footer></footer>
        {{ script('jQuery-2-0-3.min.js') }}
        {{ script('bootstrap.js') }}
    </body>
    </html>
    

    Add this to your base controller: It gets called automatically by Laravel if it exists

    protected function setupLayout()
    {
    
        $this->layout = View::make('layouts.master');
    
    }
    

    Controller method (nesting views)

    public function index()
    {
    
        $this->layout->content = View::make('public.interior.index')
                                    ->nest('content', 'components.login')
                                    ->nest('sideBar', 'components.sidebars.interiorSidebar1', ['extra' => View::make('components.sidebars.extra.extra1')]);
    
    }
    

    Index view (parent view.. @section defined):

    @section('content')
    
        <div class="row-fluid col-md-7 col-sm-12 col-md-offset-1 col-sm-offset-0">
            {{ $content }}
        </div>
    
        <div class="row-fluid col-md-3 col-sm-12 col-md-offset-1 col-sm-offset-0 pull-right">
            {{ $sideBar }}
        </div>
    
    @stop
    

    Nested view (component type stuff. no @section defined)

    {{ Form::open(['class' => 'form-horizontal', 'role' => 'form']) }}
        <h2>User Login</h2>
        <div class="form-group">
            {{ Form::label('email', 'Email:', ['class' => 'col-sm-2 control-label']) }}
            <div class="col-sm-10">
                {{ Form::text('email', null, ['id' => 'email','class' => 'form-control']) }}
            </div>
        </div>
        <div class="form-group">
            {{ Form::label('password', 'Password:', ['class' => 'col-sm-2 control-label']) }}
            <div class="col-sm-10">
                {{ Form::text('password', null, ['class' => 'form-control']) }}
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                {{ Form::submit('Login', ['class' => 'btn btn-primary']) }}
                 <a href="#" class="btn btn-default">Forgot Password</a>
            </div>
        </div>
    
    {{ Form::close() }}
    

    Then the oh, so magical part... View composers: Create a composers.php file and include it to bind data to certain views

    View::composer(['layouts.master'], function($view){
    
        if(Auth::check()){
            $authClass = 'logged-in';
        } else {
            $items          = MenuMaker::getPublic();
    
            $authClass = 'logged-out';
    
            $view->with('mainNav', View::make('components.mainNavPublic', ['items' => $items]))
                    ->with('authClass', $authClass);
        }
    
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛