Mike
Mike

Reputation: 530

How can i call an element in cakePHP?

i need to call a cakePHP element in an Ajax function; when the user clicks an item want to populate a DIV with the content of an element. The problem is that i don't know how to properly create that element because i call the url but cakephp renders all the webpage instead of only the element. How can i call only the element through a controller action?

Thanks in advance c.

Upvotes: 2

Views: 6867

Answers (3)

chetanspeed511987
chetanspeed511987

Reputation: 2025

$this->layout = NULL;
$this->autoRender = false;


// your code



$this->render('/elements/myElement');

Upvotes: 2

Anh Pham
Anh Pham

Reputation: 5481

faster way: $this->render('/elements/myElement','ajax'); at the end of your controller action.

Upvotes: 3

Dave
Dave

Reputation: 29121

Try calling an action, but within it, specify:

$this->layout = false;

That makes it so it does not use a layout file.

Then, within the view of that action, just echo the element - nothing else:

<?php
echo $this->element('myElement');
?>

Upvotes: 4

Related Questions