Laravel 5添加到父视图部分


Laravel 5 prepend to parent view section

我正在使用Laravel 5.0编写一个网页,并希望将视图的部分模块化。目前我有硕士>>视图在祝辞部分模板。

master.blade.php

<head>
    <!-- master's css -->
    @yield('css')
</head>
<body>
    @yield('content')
</body>

view.blade.php

@extends('master')
@section('css')
    <!-- view-specific css -->
@stop
@section('content')
    <!-- some content -->
    @include('partial')
@stop

partial.blade.php

@section('css')
    @parent
    <!-- partial-specific css -->
@append
<!-- partial's content -->

加载方式:

<head>
    <!-- master's css -->
    <!-- view-specific css -->
    <!-- partial-specific css -->
</head>

加载方式:

<head>
    <!-- master's css -->
    <!-- partial-specific css -->
    <!-- view-specific css -->
</head>

我已经尝试移动@parent之前和之后的view-specific css,但页面加载partial-specific css后的视图css不管。

我的理由是,我想从视图模板的最后一个css表是最后加载,这样我可以覆盖部分的css,如果需要使用它的特定页面。

更好的方法是让partial扩展view:

<head>
    <!-- master's css -->
    @yield('css')
</head>
<body>
    @yield('content')
</body>
<<p> 视图/strong>
@extends('master')
@section('css')
    <!-- view-specific css -->
@stop
@section('content')
    <!-- some content -->
@stop

部分

@extends('view')
@section('css')
    <!-- partial-specific css -->
    @parent
@stop
@section('content')
    @parent
    <!-- partial's content -->
@stop

你的输出将是:

<head>
    <!-- master's css -->
    <!-- partial-specific css -->
    <!-- view-specific css -->
</head>
<body>
        <!-- some content -->
<!-- partial's content -->
</body>

而不是return view('view');
return view('partial');

这样做在你的情况下可能没有意义,但你会得到想要的结果。