Well, the title might be a bit confusing, so let me put some light on the situation. I have a base template in Blade containing basic html and navigation, which looks like this:

<!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>

And then a bunch of child views for each page, which go like this:

@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

Now, what I want is being able to conditionally ignore the @extends(), so that I can return a view which is just the child view, that is everything within section of class "container", for being able to stack it up into one-page layout or load using AJAX.

Any idea on how to achieve this?

有帮助吗?

解决方案

You can use something like this

@extends(((condition) ? 'layouts.plain' : 'layouts.base'))

Now create a layout named plain and keep nothing inside it but just content, this way you can use a plain layout, for example, check this answer.

//layouts/plain.blade.php
@yield('content')

其他提示

One possible solution:

  1. pass the layout to the View as a variable ($layout), rather than defining a string in the view.
  2. create a second layout with only the content section defined.
  3. then you can call the View::make() from anywhere with the template set according to your needs.

Change the @extends in the child view:

@extends((isset($layout)) ? $layout : 'layouts.base')

create your new "content only" layout: /app/views/layouts/content-only.blade.php:

// only this line in the file:
@yield('content')

then in normal use:

return View::make('child-view');

and when you want the content-only version:

return View::make('child-view')->with('layout', 'layouts.content-layout');

EDIT - updated to take into account the tip in Sheikh Heera's answer!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top