在 Laravel 4 / Blade 中将布局分解为部分
Posted
技术标签:
【中文标题】在 Laravel 4 / Blade 中将布局分解为部分【英文标题】:Breaking layouts into partials in Laravel 4 / Blade 【发布时间】:2014-10-14 01:49:49 【问题描述】:无法将我的观点分解成一些不那么多余的东西。现在,这是典型布局的外观:
master.blade.php:
<!DOCTYPE html>
<html>
<head>
<title>
@section('title')
@show
</title>
<script type="text/javascript" src=" asset('bower/jquery/dist/jquery.min.js') "></script>
<link href=" asset('bower/bootstrap/dist/css/bootstrap.min.css') " rel="stylesheet">
<script type="text/javascript" src=" asset('bower/bootstrap/dist/js/bootstrap.min.js') "></script>
<script type="text/javascript" src=" asset('bower/ckeditor/ckeditor.js') "></script>
<link href=" asset('css/default.css') " rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row center-block text-center indexWrapper">
<div class="indexNav">
<ul class="text-right">
<li><a href="URL::to('people')">People</a></li>
<li><a href="URL::to('bulletin')">Bulletin</a></li>
<li><a href="URL::to('current')">Current</a></li>
<li><a href="URL::to('finished')">Finished</a></li>
</ul>
</div>
<div class="indexHeading">
<h1 class="indexH1">
@section('navTitle')
@show
</h1>
</div>
<div class="clearfix"></div>
</div>
@yield('content')
<div class="center-block login">
@yield('login')
</div>
</div>
<div class="row center-block footer">
<hr>
<ul>
<small>
<li><span style="color:red">DEVELOPMENT MODE</span></li>
<li>Mumble © 2014</li>
<li><a href="">Follow project on GitHub</a></li>
</small>
</ul>
</div>
@section('scripts')
@show
</body>
</html>
然后,扩展它,是单个页面视图。不过,我想将主布局的页眉和页脚分开
在刀片中进行高效模板的最佳方法是什么?
【问题讨论】:
【参考方案1】:基本功能是使用包含,这将抓取要包含的文件,并将不是内容放在包含标记的位置。
master.blade.php(放在“views/”文件夹下)
<!DOCTYPE html>
<html>
<head>
<title>
@section('title')
@show
</title>
@include('layout.header')
</head>
<body>
<div class="container">
<div class="row center-block text-center indexWrapper">
<div class="indexNav">
<ul class="text-right">
<li><a href="URL::to('people')">People</a></li>
<li><a href="URL::to('bulletin')">Bulletin</a></li>
<li><a href="URL::to('current')">Current</a></li>
<li><a href="URL::to('finished')">Finished</a></li>
</ul>
</div>
<div class="indexHeading">
<h1 class="indexH1">
@section('navTitle')
@show
</h1>
</div>
<div class="clearfix"></div>
</div>
@yield('content')
<div class="center-block login">
@yield('login')
</div>
</div>
<div class="row center-block footer">
<hr>
<ul>
<small>
<li><span style="color:red">DEVELOPMENT MODE</span></li>
<li>Mumble © 2014</li>
<li><a href="">Follow project on GitHub</a></li>
</small>
</ul>
</div>
@section('scripts')
@show
</body>
</html>
header.blade.php(放在“views/layout/”文件夹下)
<head>
<title>
@section('title')
@show
</title>
<script type="text/javascript" src=" asset('bower/jquery/dist/jquery.min.js') "></script>
<link href=" asset('bower/bootstrap/dist/css/bootstrap.min.css') " rel="stylesheet">
<script type="text/javascript" src=" asset('bower/bootstrap/dist/js/bootstrap.min.js') "></script>
<script type="text/javascript" src=" asset('bower/ckeditor/ckeditor.js') "></script>
<link href=" asset('css/default.css') " rel="stylesheet">
</head>
footer.blade.php 遵循与页脚页眉相同的原则。
希望这有帮助!
【讨论】:
完美。您是否会说这会对加载速度产生重大影响(负面或正面)? 我还没有真正测试过,但它可能会......因为您需要获取两个或三个多个文件来创建一个页面。您可以使用 chome 或 firefox 中的网络工具检查这一点,并检查请求和页面加载之间的延迟。以上是关于在 Laravel 4 / Blade 中将布局分解为部分的主要内容,如果未能解决你的问题,请参考以下文章
php Laravel刀片布局。了解如何使用Blade在Laravel中快速创建布局:https://www.cloudways.com/blog/create-laravel-bl