Pixy
Pixy

Reputation: 1138

CakePHP: Cannot set a variable when calling an element

I'm looping over a set of products like this

<?php foreach ($results as $result): ?>
   <?php echo $this->element('result_item', array('product', $result)); ?>
<?php endforeach; ?>

and here's an excerpt of my element code:

[...]
<?php echo $this->Html->url(array('controller' => 'view', 'action' => 'index', $product['Product']['product_slug'])) ?>
[...]

I'm getting the following error each I try to access $product

Notice (8): Undefined variable: product [APP/View/Elements/result_item.ctp, line 2]/view">

What am I doing wrong?

Upvotes: 0

Views: 715

Answers (2)

Narendra Vaghela
Narendra Vaghela

Reputation: 289

Use array to pass variables to element.

See doc here: http://book.cakephp.org/2.0/en/views.html#passing-variables-into-an-element

Upvotes: 0

Ben Hitchcock
Ben Hitchcock

Reputation: 1378

You're passing through two arguments, instead of one named argument.

Change your view code to be this:

<?php foreach ($results as $result): ?>
   <?php echo $this->element('result_item', array('product' => $result)); ?>
<?php endforeach; ?>

(Note the => instead of , in the element call)

Upvotes: 1

Related Questions