Laravel 5 - 仅在特定页面/控制器(页面特定资产)上添加样式表

12 laravel blade laravel-4 laravel-5

我的Laravel布局目前如下(删除导航栏等..):

<link href="//maxcdn.bootstrapcdn.com/bootswatch/3.3.4/flatly/bootstrap.min.css" 
     rel="stylesheet">
<link href="{{ asset('/css/app.css') }}" rel="stylesheet">

<!-- Fonts -->
<link href='//fonts.googleapis.com/css?family=Roboto:400,300' rel='stylesheet' 
    type='text/css'>

<!-- Scripts -->
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>

    <div class="container">

        @yield('content')

    </div>

    <!-- Scripts -->
    <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
    <script src="{{ asset('assets/vendor/toastr/toastr.min.js') }}"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

然后我附上一个视图布局:

@extends('app')

@section('content')

Content here.

@endsection
Run Code Online (Sandbox Code Playgroud)

我很困惑,例如,如果我在clinicController.php诊所路线内(或它是一个资源),然后将样式表和Javascript文件"注入"到适当的位置.

有没有办法做到这一点?我想这@if on a certain page, display this会有用,但我确定必须有一个更"Laravel"的方法来做到这一点?

任何帮助将非常感激.

The*_*pha 34

您可以创建section类似以下的内容view,例如:

@extends('layouts.master')

@section('styles')
    <link href="{{asset('assets/css/custom-style.css')}}" />
@stop
Run Code Online (Sandbox Code Playgroud)

然后还你layout,@yield是,例如:

<!--Static StyleSheets-->
<link href="{{asset('assets/css/common-style.css')}}" rel="stylesheet" />

<!--Dynamic StyleSheets added from a view would be pasted here-->
@yield('styles')
Run Code Online (Sandbox Code Playgroud)

script例如,标签也是如此(A layout可能看起来像styles/ scripts):

<html>
    <head>
        <!--Static StyleSheets-->
        <link href="{{asset('assets/css/common-style.css')}}" />

        <!--Dynamic StyleSheets added from a view would be pasted here-->
        @yield('styles')
    </head/>

    <!-- Template Body -->
    <body>
        <!-- Other HTML Elements -->
        <div class="container">
            @yield('content')
        </div>

        <!-- Dump all dynamic scripts into template -->
        @yield('scripts')
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

更新:从Laravel 5.2开始,可以将堆栈用于styles/ scripts,例如:

在视图中:

@extends('layouts.master')

@section('content')
    <!-- Content -->
@endsection

<!-- Push a style dynamically from a view -->
@push('styles')
    <link href="{{asset('assets/css/common-style.css')}}" />
@endpush

<!-- Push a script dynamically from a view -->
@push('scripts')
    <script src="/example.js"></script>
@endpush
Run Code Online (Sandbox Code Playgroud)

模板:

<html>
    <head>
        <!--Static StyleSheets-->
        <link href="{{asset('assets/css/common-style.css')}}" />

        <!--Dynamic StyleSheets added from a view would be pasted here-->
        @stack('styles')
    </head/>
    <!-- Template Body -->
    <body>

        <!-- Other HTML Elements -->
        <div class="container">
            @yield('content')
        </div>

        <!-- Dump all dynamic scripts into template -->
        @stack('scripts')

    </body>
</html>
Run Code Online (Sandbox Code Playgroud)


Sta*_*zie 6

如果您像我一样,不喜欢将脚本分散在多个视图文件中的想法,则可以使用以下任意一种方式在模板中以可维护的方式来实现它:

如果使用命名路由

@if (in_array(Route::currentRouteName(), ['profile', 'register']))
    <script src="example.js"></script>
@endif
Run Code Online (Sandbox Code Playgroud)

(同上)

@if (in_array(\Request::route()->getName(), ['profile', 'register']))
    <script src="example.js"></script>
    <script src="another.js"></script>
@endif
Run Code Online (Sandbox Code Playgroud)

而是检查控制器动作

  @if (in_array(substr(Route::currentRouteAction(), strpos(Route::currentRouteAction(), '@')+1), ['create', 'edit']))
    <script src="example.js"></script>
  @endif
Run Code Online (Sandbox Code Playgroud)

指定控制器和动作

  @if (in_array(substr(Route::currentRouteAction(), strpos(Route::currentRouteAction(), 'Controllers')+12), ['UserController@create', 'AlbumController@edit']))
    <script src="example.js"></script>
  @endif
Run Code Online (Sandbox Code Playgroud)