Conditional extends in Blade

后端 未结 3 1892
后悔当初
后悔当初 2021-01-04 03:04

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

What I\'ve tried:

@if(!Request::ajax())
             


        
相关标签:
3条回答
  • 2021-01-04 03:16

    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;
        }
    }
    
    0 讨论(0)
  • 2021-01-04 03:24
    @extends((( Request::ajax()) ? 'layouts.ajax' : 'layouts.default' ))
    
    0 讨论(0)
  • 2021-01-04 03:29

    in the master layout:

       @if(!Request::ajax())
    
           //the master layout with @yield('content'). i.e. your current layout
    
       @else
    
           @yield('content')
    
       @endif
    
    0 讨论(0)
提交回复
热议问题