emersonthis emersonthis - 5 months ago 11
PHP Question

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

I'm trying to figure out how to use one of my view elements inside of a controller...

I know, I know: "Don't do that!" (99% of the time this is the correct answer)

But I think I actually have a good reason. The action is handling an AJAX request which returns markup. The returned markup is a list which I display everywhere else using an element. So in an effort to keep my code DRY, I think it's appropriate to do this here.

Is this possible?

Answer

Easy:

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

Also:

DON'T DO THAT ANYMORE!!!

Comments