Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional extends in Blade

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

like image 361
Jordan Doyle Avatar asked Aug 30 '13 03:08

Jordan Doyle


People also ask

What is Blade syntax?

The Blade is a powerful templating enginetemplating engineTemplate system may refer to: Template processor, software designed to combine templates with a data model to produce result documents. Web template system, a system that allows web designers and developers work with web templates to automatically generate custom web pages.https://en.wikipedia.org › wiki › Template_systemTemplate system - Wikipedia in a Laravel framework. The blade allows to use the templating engine easily, and it makes the syntax writing very simple. The blade templating engine provides its own structure such as conditional statements and loops.

What is Blade in PHP?

Blade is the simple, yet powerful templating enginetemplating engineTemplate system may refer to: Template processor, software designed to combine templates with a data model to produce result documents. Web template system, a system that allows web designers and developers work with web templates to automatically generate custom web pages.https://en.wikipedia.org › wiki › Template_systemTemplate system - Wikipedia that is included with Laravel. Unlike some PHP templating engines, Blade does not restrict you from using plain PHP code in your templates.

How do you call a component in Laravel?

To generate such a view component, you can call php artisan make:component Alert . This will generate two files for you: resources/views/components/alert.


3 Answers

@extends((( Request::ajax()) ? 'layouts.ajax' : 'layouts.default' ))
like image 96
Christopher Raymond Avatar answered Oct 04 '22 09:10

Christopher Raymond


in the master layout:

   @if(!Request::ajax())

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

   @else

       @yield('content')

   @endif
like image 21
itachi Avatar answered Oct 04 '22 09:10

itachi


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;
    }
}
like image 41
Adam Lavin Avatar answered Oct 04 '22 07:10

Adam Lavin