Laravel 5-仅在特定页面/控制器上(页面特定资产)添加样式表 [英] Laravel 5 - Add a stylesheet only if on a certain page/controller (page specific asset)

查看:49
本文介绍了Laravel 5-仅在特定页面/控制器上(页面特定资产)添加样式表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

<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

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

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

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

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

任何帮助将不胜感激.

推荐答案

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

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

@extends('layouts.master')

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

然后在您的layout@yield中,例如:

Then also in your layout, @yield that, for example:

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

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

例如,script标签也是如此(styles/scripts可能与此类似):

Same goes for script tags, for example (A layout may look like this with 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>

更新:自Laravel 5.2起,可以使用堆栈styles/scripts的a>,例如:

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

在视图中:

@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

模板:

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

这篇关于Laravel 5-仅在特定页面/控制器上(页面特定资产)添加样式表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆