开发者

Only render specified view - CakePHP

开发者 https://www.devze.com 2023-02-16 06:47 出处:网络
I would like to render the view by itself, I 开发者_如何学编程want to avoid rendering the <head> and other sections defined in the default.ctp file.

I would like to render the view by itself, I 开发者_如何学编程want to avoid rendering the <head> and other sections defined in the default.ctp file.

I have the info that I want to render in an element:

<?php

echo $post['Post']['id'];

?>

How can I accomplish this? It is for an ajax response.

Thanks!


You can create a new layout (/app/views/layouts/ajax.ctp, for example) that contains:

<?php echo $content_for_layout ?>

Then in your controller, in the action you are using, define the layout:

function myaction() {
    $this->layout = 'ajax';
}

Reference: http://book.cakephp.org/view/962/Page-related-Attribute-layout


Just create a layout called ajax.ctp which only contains

<?php
echo $content_for_layout;

then in the controller put

$this->layout = 'ajax';

If you want to do this programmatically for all AJAX requests, just add the RequestHandler component to your controller

var $components = array('RequestHandler'); //and possibly others

and create a beforeRender method like

function beforeRender() {
    if ($this->RequestHandler->isAjax()) {
        $this->layout = 'ajax';
        Configure::write('debug', 0);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消