Laravel 4 Blade Templating Engine with Conditional

2019-04-02 11:16发布

问题:

I'm using L4 with Blade. I'd like to be able to conditionally extend a layout. For normal use, I'd like to extend the master layout, and for ajax renders, I'd like to be able to extend the ajax template. I use the following code:

@if ( isset($ajax) )
    @extends('layouts.ajax')
@else
    @extends('layouts.master')
@endif

But when the page renders it just prints out @extend('layouts.master').

Does anyone know how to conditionally extend a layout or another?

Thanks

回答1:

Try on the first line:

@extends('layouts.' . isset($ajax) ? 'ajax' : 'master')

EDIT

You also can use it this way:

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