Rendering and not rendering

Posted on 10/21/09 by Rob Conner
One of the most common questions I see on IRC is "How do I stop the view from displaying?".

The controller has a handy little attribute <em>autoRender</em>, setting it to false will stop the view from being displayed.

Also using the render(...) method automatically sets autoRender to false. This is handy if you want to another view aside from the default one for an action.

Some example code:
Code:
<?php // in a controllers action $this->autoRender = false; header('Content-type: application/pdf'); readfile('/path/tp/pdf_file'); // or say you want to render an element as a view. // maybe you are using ajax if($this->RequestHandler->isAjax()) { // is a cake component $this->viewPath = 'elements'.DS.'products'; $this->render('product_table', 'ajax'); } ?>

Additional links:
Controller::render [api.cakephp.org]
RequestHandler [api.cakephp.org]

-rtconner