3

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
Mike Rockétt
  • 8,947
  • 4
  • 45
  • 81
max
  • 3,614
  • 9
  • 59
  • 107

2 Answers2

4

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.

Community
  • 1
  • 1
Mike Rockétt
  • 8,947
  • 4
  • 45
  • 81
  • @MikeAnthony thanx , `@layout` used to work in the version 4 beta ... actually i'm using blade with codeigniter ! – max Jul 31 '13 at 19:36
3

IMO, checking for ajax requests is not the view's responsibility, but the controller's.

What if you created two files, one for normal requests and other for ajax requests, and then have one including the other?

For instance:

Template file mypage.blade.php

@layout('master')

@section('mainContent')
    @include('mypage.ajax')
@endsection

Template file mypage.ajax.blade.php

my contents here

Then, its just a matter of calling view::make('mypage') or view::make('mypage.ajax').

pedromanoel
  • 3,232
  • 2
  • 24
  • 23