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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30716599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:44:15  来源:igfitidea点击:

Laravel 5 - Add a stylesheet only if on a certain page/controller (page specific asset)

laravellaravel-4laravel-5blade

提问by The Alpha

My Laravel layout is currently as follows (removed navbar etc..):

我的 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>

Then I attach a view to the layout:

然后我将视图附加到布局:

@extends('app')

@section('content')

Content here.

@endsection

I'm confused as to, say for example, if I'm in the clinicController.phpor within a clinic route (it's a resource), then to "inject" a stylesheet and a Javascript file into the appropriate places.

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

Is there a way to do this? I'd imagine that @if on a certain page, display thiswould work, but I'm sure there must be a more "Laravel" way to do it?

有没有办法做到这一点?我想这@if on a certain page, display this会奏效,但我确定必须有一种更“Laravel”的方式来做到这一点?

Any help would be hugely appreciated.

任何帮助将不胜感激。

回答by The Alpha

You may create a sectionlike the following in your view, for example:

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

@extends('layouts.master')

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

Then also in your layout, @yieldthat, for example:

然后也在您的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')

Same goes for scripttags, for example (A layoutmay look like this with styles/ scripts):

script例如,标签也是如此(Alayout可能看起来像这样,带有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>

Update: Since Laravel 5.2, it's possible to use Stacksfor styles/scripts, for example:

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

In A View:

在视图中:

@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

The Template:

模板:

<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>

回答by Stanley Umeanozie

If you are like me and do not like the idea of littering the scripts in multiple view files, you can do it in a maintainable fashion in your template using any one of the following:

如果您像我一样不喜欢在多个视图文件中乱扔脚本的想法,您可以使用以下任何一种在模板中以可维护的方式进行:

If Using Named Routes

如果使用命名路由

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

OR(Same as above)

(同上)

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

To Check For Controller Actions Instead

改为检查控制器操作

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

To Specify a Controller And an Action

指定控制器和动作

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