CakePHP: How to use a view element inside of a controller

Easy:

$view = new View($this, false);
$content = $view->element('my-element', $params);

Also:

DON'T DO THAT ANYMORE!!!


Sometimes, you need to render a CakePhp element from a view and inject its content into the page using AJAX the same time. In this case rendering element as a regular view from controller is better than creating a dedicated view that just contains <?php echo $this->element('some_element') ?>, and may be done this way:

<?php
public function ajax_action() {
    // set data used in the element
    $this->set('data', array('a'=>123, 'b'=>456, 'd'=>678));

    // disable layout template
    $this->layout = 'ajax';

    // render!
    $this->render('/Elements/some_element');
}