Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I display ajax results in a view in CakePHP without including the default template in the ajax response?

Tags:

php

cakephp

I'm doing some experimenting with AJAX in CakePHP and it seems to work except that the view that gets returned includes the default template. How can I get rid of that (or even just specify a different empty template for a view)?

like image 387
tooshel Avatar asked May 05 '09 21:05

tooshel


3 Answers

function ajaxFunction() {
    //do stuff
    $this->layout= 'ajax';
}

Ajax is an included blank layout to prevent extra markup being added, exactly what you want.

http://book.cakephp.org/view/96/Layouts

like image 179
seanmonstar Avatar answered Nov 16 '22 19:11

seanmonstar


Try using RequestHandler component. This will be handled automatically for you. Then, you can do something like this in your AppController::beforeFilter()

if($this->RequestHandler->isAjax()) {
    Configure::write('debug',0);
}
like image 42
jmcneese Avatar answered Nov 16 '22 18:11

jmcneese


You will also need to turn off debug output otherwise cake will squirt out all the debug info you usually see at the bottom of the page:

function ajaxFunction() {
    //do stuff
    Configure::write('debug', 0);
    $this->layout= 'ajax';
}
like image 28
RichardAtHome Avatar answered Nov 16 '22 17:11

RichardAtHome