Search code examples
laravellaravel-3

Laravel's blade : showing template without its layout if it's a ajax request


so i'm using blade for my templates (apparently not the latest version but it's not important)

@layout('master')

@section('mainContent')
my contents here
@endsection

it works great but the thing is when im calling the page via ajax request i dont want the layout to show up , only mainContent section

something like this .... perhaps cleaner

@if(!$is_ajaxCall)
@layout('master')
@endif

@if(!$is_ajaxCall)
@section('mainContent')
@endif
my contents here

@if(!$is_ajaxCall)
@endsection
@endif

Solution

  • You're very close indeed. Use Request::ajax() to determine this.

    @if(!Request::ajax())
        @layout('master')
    @endif
    

    You might also consider using Blade Extensions to do this. You'd need to create the Blade Extension (say, from your before filter), and then call it. Then, you can make a call that looks something like this:

    @layoutNoAjax('master')
    

    To learn how to extend Blade, see this article: http://blog.zerilliworks.net/blog/2013/04/03/blade-extensions-in-laravel/

    Update

    My mistake. Please see this answer: https://stackoverflow.com/a/15170353/1626250

    Also, you tagged laravel-4, which used @extends, and not @layout.