好吧,标题可能有点令人困惑,所以让我来说明一下情况。我在Blade中有一个包含基本html和导航的基本模板,看起来像这样:
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
@section('title')
<title>Page Title</title>
@show
</head>
<body>
<div id="wrapper">
<header id="logo"></header>
<nav id="mainmenu">
<ul>
<li><a href="page1">Page 1</a></li>
<li><a href="page2">Page 2</a></li>
<li><a href="page3">Page 3</a></li>
<li><a href="page4">Page 4</a></li>
</ul>
</nav>
@yield('content')
</div>
</body>
</html>
然后每个页面都有一堆子视图,如下所示:
@extends('layouts.base')
@section('title')
<title>Page 1</title>
@stop
@section('content')
<section class="container clearfix">
<h2 class="section-title">Page 1</h2>
<div class="content">
some content here
</div>
</section>
@stop
现在,我想要的是能够有条件地忽略@extends(),这样我就可以返回一个只是子视图的视图,即类"容器"部分中的所有内容,以便能够将其堆叠到一个页面布局中或使用AJAX加载。
你知道如何做到这一点吗?
您可以使用类似的东西
@extends(((condition) ? 'layouts.plain' : 'layouts.base'))
现在创建一个名为plain
的布局,只保留内容,这样就可以使用普通布局,例如,检查这个答案。
//layouts/plain.blade.php
@yield('content')
一种可能的解决方案:
- 将布局作为变量(
$layout
)传递给视图,而不是在视图中定义字符串 - 创建仅定义了
content
部分的第二个布局 - 然后您可以使用根据您的需要设置的模板从任何地方调用CCD_ 4
更改子视图中的@extends
:
@extends((isset($layout)) ? $layout : 'layouts.base')
创建新的"仅限内容"布局:/app/views/layouts/content-only.blade.php:
// only this line in the file:
@yield('content')
然后在正常使用中:
return View::make('child-view');
当你想要只包含内容的版本时:
return View::make('child-view')->with('layout', 'layouts.content-layout');
编辑-根据Sheikh Heera回答中的提示进行更新!