Conditional extends in Blade

吃可爱长大的小学妹 提交于 2019-12-10 01:08:43

问题


Is there any way to do a conditional @extends statement in the Blade templating language?

What I've tried:

@if(!Request::ajax())
    @extends('dashboard.master')
    @section('content')
@endif

<div class="jumbotron">
    Hey!
</div>

@if(!Request::ajax())
    @stop
@endif

Output

When the request was not AJAX it printed out @extends('dashboard.master'), but the AJAX request worked fine.

What I'm trying to do

Stop including the master template (which includes header and footer) for AJAX so it can easily display the requested content


回答1:


in the master layout:

   @if(!Request::ajax())

       //the master layout with @yield('content'). i.e. your current layout

   @else

       @yield('content')

   @endif



回答2:


@extends((( Request::ajax()) ? 'layouts.ajax' : 'layouts.default' ))



回答3:


This kind of logic should really be kept out of the template.

In your controller set the $layout property to be dashboard.master then instead of calling returning your view or response, terminate with just $this->layout->content = View::make('dashboard.template')

Take a look at the Laravel docs on this

You could end up with something like this

<?php

class Something extends BaseController {

    $layout = 'dashboard.master';

    public function getIndex()
    {
        $template = View::make('dashboard.template');

        if(Request::ajax()) {
            return $template;
        }

        $this->layout->content = $template;
    }
}


来源:https://stackoverflow.com/questions/18524365/conditional-extends-in-blade

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!