Laravel 5 - 仅在特定 page/controller(页面特定资产)上添加样式表
Laravel 5 - Add a stylesheet only if on a certain page/controller (page specific asset)
我的 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>
然后我将视图附加到布局:
@extends('app')
@section('content')
Content here.
@endsection
我很困惑,例如,如果我在 clinicController.php
或诊所路线内(这是一种资源),那么 "inject" 样式表和 Javascript 文件放到合适的地方。
有办法吗?我想 @if on a certain page, display this
会起作用,但我确定必须有更多 "Laravel" 的方法来做到这一点?
如有任何帮助,我们将不胜感激。
您可以在 view
中创建如下所示的 section
,例如:
@extends('layouts.master')
@section('styles')
<link href="{{asset('assets/css/custom-style.css')}}" />
@stop
然后也在你的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')
同样适用于 script
标签,例如(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>
更新:由于Laravel 5.2,可以使用Stacks作为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
模板:
<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>
如果您像我一样不喜欢在多个视图文件中乱扔脚本的想法,您可以使用以下任一方法在模板中以可维护的方式进行:
如果使用命名路由
@if (in_array(Route::currentRouteName(), ['profile', 'register']))
<script src="example.js"></script>
@endif
或(同上)
@if (in_array(\Request::route()->getName(), ['profile', 'register']))
<script src="example.js"></script>
<script src="another.js"></script>
@endif
改为检查控制器操作
@if (in_array(substr(Route::currentRouteAction(), strpos(Route::currentRouteAction(), '@')+1), ['create', 'edit']))
<script src="example.js"></script>
@endif
指定控制器和动作
@if (in_array(substr(Route::currentRouteAction(), strpos(Route::currentRouteAction(), 'Controllers')+12), ['UserController@create', 'AlbumController@edit']))
<script src="example.js"></script>
@endif
我的 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>
然后我将视图附加到布局:
@extends('app')
@section('content')
Content here.
@endsection
我很困惑,例如,如果我在 clinicController.php
或诊所路线内(这是一种资源),那么 "inject" 样式表和 Javascript 文件放到合适的地方。
有办法吗?我想 @if on a certain page, display this
会起作用,但我确定必须有更多 "Laravel" 的方法来做到这一点?
如有任何帮助,我们将不胜感激。
您可以在 view
中创建如下所示的 section
,例如:
@extends('layouts.master')
@section('styles')
<link href="{{asset('assets/css/custom-style.css')}}" />
@stop
然后也在你的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')
同样适用于 script
标签,例如(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>
更新:由于Laravel 5.2,可以使用Stacks作为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
模板:
<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>
如果您像我一样不喜欢在多个视图文件中乱扔脚本的想法,您可以使用以下任一方法在模板中以可维护的方式进行:
如果使用命名路由
@if (in_array(Route::currentRouteName(), ['profile', 'register']))
<script src="example.js"></script>
@endif
或(同上)
@if (in_array(\Request::route()->getName(), ['profile', 'register']))
<script src="example.js"></script>
<script src="another.js"></script>
@endif
改为检查控制器操作
@if (in_array(substr(Route::currentRouteAction(), strpos(Route::currentRouteAction(), '@')+1), ['create', 'edit']))
<script src="example.js"></script>
@endif
指定控制器和动作
@if (in_array(substr(Route::currentRouteAction(), strpos(Route::currentRouteAction(), 'Controllers')+12), ['UserController@create', 'AlbumController@edit']))
<script src="example.js"></script>
@endif