如何使用Laravel刀片模板引擎的 @include
加载页面特定资源?
下面是我的Master layout ( master.blade.php
)的内容:
<head>
@section('styles')
{{-- Some Master Styles --}}
@show
</head>
<body>
{{-- Header --}}
@section('header')
@include('header')
@show
{{-- Content --}}
@section('content')
{{-- Content for page is extending this view --}}
@show
{{-- Footer --}}
@section('footer')
@include('footer')
@show
</body>
在给定的页面中,我是这样使用主模板的:
@extends('master')
@section('styles')
@parent
{{-- Page Stylesheet --}}
@endsection
上面的方法是我用来尝试加载我的页面特定的样式到<head>
部分。
不能正常工作
我也想加载其他页面特定的资源(s)在我的页脚使用相同的方法;我怎样才能有效地做到这一点?
你不需要做
@extends('master')
@section('styles')
@parent
{{-- Page Stylesheet --}}
@endsection
,以便加载特定页面的样式表。
您应该为您的 master.blade.php
文件加载特定于页面的样式表,以便保持代码干燥。
为此,您需要指定这些页面的路由或期望的url格式,然后加载相应的样式表。
您可以在 master.blade.php
文件中这样做:
@section('styles')
@if(Request::is('transactions/generate-invoice'))
@include('generate-invoice-css')
@elseif(Request::is('transactions/users'))
@include('users-css')
@endif
@show
其中 generate-invoice-css.blade.php
包含您希望加载的用于 yoursite.com/transactions/generate-invoice
和 users-css.blade.php
页面内容的样式表, yoursite.com/transactions/users
的样式表。
transactions
下的页面的相同样式表,您可以这样做:
@if(Request::is('transactions*'))
使用一个通配符 *
。
要将给定资源加载到页面的<head>
部分以外的位置,只需使用相同的方法并根据需要进行调整。
要从 master.blade.php
中用 @include()
加载特定页面的资源,请使用以下方法(在 master.blade.php
文件中):
@section('styles')
@include('styles')
@show
其中 styles.blade.php
应包含加载满足您的需求的适当资源的条件,如:
@if(Request::is('transactions/generate-invoice'))
@include('generate-invoice-css')
@elseif(Request::is('transactions/users'))
@include('users-css')
@endif
作为您的 styles.blade.php
的内容。