我的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中或在诊所路由(这是一种资源)中,然后将样式表和Javascript文件“注入”到适当的位置,这让我感到困惑。
有没有办法做到这一点?我想@if on a certain page, display this可以工作,但我相信一定有更"Laravel“的方式来做?
任何帮助都将不胜感激。
发布于 2015-06-09 02:53:35
您可以在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>发布于 2017-05-30 05:19:19
如果你和我一样,不喜欢把脚本放在多个视图文件中,你可以在你的模板中以可维护的方式使用以下任何一种方式来实现:
如果使用命名路由,则为
@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>
@endifhttps://stackoverflow.com/questions/30716599
复制相似问题