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

20.7k views Asked by At

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

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.

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?

Any help would be hugely appreciated.

2

There are 2 answers

7
The Alpha On BEST ANSWER

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

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

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>

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

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>
0
Stanley Umeanozie On

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