2015-06-08 14 views
7
layout di

mio laravel è attualmente la seguente (rimosso barra di navigazione, ecc ..):laravel 5 - Aggiungere un foglio di stile solo se su una certa pagina/regolatore (asset specifica pagina)

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

Poi ho allegare al fine di il layout:

@extends('app') 

@section('content') 

Content here. 

@endsection 

sono confuso da, diciamo per esempio, se sono in clinicController.php o all'interno di un percorso clinica (si tratta di una risorsa), poi a "iniettare" un foglio di stile e un file JavaScript nei posti appropriati.

C'è un modo per farlo? Immagino che lo @if on a certain page, display this funzionerebbe, ma sono sicuro che ci deve essere un modo più "Laravel" per farlo?

Qualsiasi aiuto sarebbe molto apprezzato.

risposta

21

Si può creare una section come il seguente nella vostra view, ad esempio:

@extends('layouts.master') 

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

Poi anche nel vostro layout, @yield che, ad esempio:

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

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

Lo stesso vale per script tag ad esempio (A layout può avere questo aspetto con 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> 

Aggiornamento: Dal laravel 5.2, è possibile utilizzare Stacks per styles/scripts, ad esempio:

in una vista:

@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 

Il 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> 
+1

Eccellente, questo funziona perfettamente. Grazie mille @The Alpha! – Ben

+1

Siete i benvenuti :-) –

+0

Attualmente sto avendo la stessa identica difficoltà a capire questo.Quindi è esattamente il punto di ingresso per l'esempio che l'Alpha aveva in cima? – alaboudi

4

Se sei come me e non ti piace l'idea di sporcare e script in più file vista, è possibile farlo in un modo gestibile nel modello utilizzando uno qualsiasi dei seguenti:

Se Utilizzando Named Itinerari

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

O (come sopra)

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

Per verificare la presenza di azioni di controllo Invece

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

per specificare un controller e un'azione

@if (in_array(substr(Route::currentRouteAction(), strpos(Route::currentRouteAction(), 'Controllers')+12), ['[email protected]', '[email protected]'])) 
    <script src="example.js"></script> 
    @endif 
Problemi correlati