我的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
或在诊所路线内(它是一种资源),那么就注意到&#34;注射& #34;将样式表和Javascript文件放入适当的位置。
有办法做到这一点吗?我想象@if on a certain page, display this
会起作用,但我确信必须有更多的&#34; Laravel&#34;这样做的方法?
非常感谢任何帮助。
答案 0 :(得分:31)
您可以在section
中创建类似以下内容的view
,例如:
@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起,可以styles
/ scripts
使用Stacks,例如:
在视图中:
@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>
答案 1 :(得分:5)
如果您像我一样并且不喜欢在多个视图文件中乱丢脚本的想法,您可以使用以下任何一种方法在模板中以可维护的方式执行此操作:
如果使用命名路线
@if (in_array(Route::currentRouteName(), ['profile', 'register']))
<script src="example.js"></script>
@endif
OR (与上述相同)
@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