普通网友 2016-09-21 13:48
浏览 34
已采纳

使用@include加载页面特定的资源

How do I load page specific resource(s) using @include of the Laravel blade template engine?

Below is the content of my Master layout (master.blade.php):

<head>
    @section('styles')
        {{-- Some Master Styles --}}
    @show
</head>
<body>
    {{-- Header --}}
    @section('header')
        @include('header')
    @show

    {{-- Content --}} 
    @section('content')
        {{-- Content for page is extending this view --}}
    @show

    {{-- Footer --}}
    @section('footer')
        @include('footer')
    @show
</body>

In a given page, I make use of my master template this way:

@extends('master')

@section('styles')
    @parent
    {{-- Page Stylesheet --}}
@endsection

The approach above is what I use to try and load my page specific style to the <head> section.

It does not work properly as expected.

I would like to as well load other page specific resource(s) in my footer using the same approach; how can I do that effectively?

  • 写回答

1条回答 默认 最新

  • drn9573 2016-09-21 14:44
    关注

    You don't need to do

    @extends('master')
    
    @section('styles')
        @parent
        {{-- Page Stylesheet --}}
    @endsection
    

    in your respective pages so as to load page specific stylesheets.

    You should rather load page specific stylesheets for your master.blade.php file so as to keep your code dry.

    To to that, you shout specify the route or expected url format of such pages, then, the appropriate stylesheet(s) to be loaded.

    You can do that this way in your master.blade.php file:

    @section('styles')
        @if(Request::is('transactions/generate-invoice'))
            @include('generate-invoice-css')
        @elseif(Request::is('transactions/users'))
            @include('users-css')
        @endif
    @show
    

    Where generate-invoice-css.blade.php contains the stylesheet(s) you want loaded for the page content accessible at yoursite.com/transactions/generate-invoice and users-css.blade.php, that of yoursite.com/transactions/users.

    For a given pattern as in: same stylesheets for pages under transactions, you can do this:

    @if(Request::is('transactions*'))
    

    using a wildcard *.

    To load a given resource to a location other than the <head> section of your pages, simply use the same approach and adapt as appropriate.

    To load page specific resources with an @include() from your master.blade.php, use this approach (in your master.blade.php file):

    @section('styles')
        @include('styles')
    @show
    

    where styles.blade.php should contain those your conditions for the appropriate resources to be loaded satisfying your requirement(s) for the purpose as in:

    @if(Request::is('transactions/generate-invoice'))
        @include('generate-invoice-css')
    @elseif(Request::is('transactions/users'))
        @include('users-css')
    @endif
    

    As the content of your styles.blade.php.

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

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制