Elements and Recursion

By Andy, filed under CakePHP, Tips, requestAction

A long time ago, I took to using elements as shown in the paste below.

echo $this->renderElement("Element",$params);
  1. echo $this->renderElement("Element",$params);
  2.  

At the time, this was the only way I could find to pass the information from the view in general to the element. If this was needed at all, it was probably a temporary glitch but I kept to this syntax since that time. It is overkill to do this now, and moreover if you use the above you may get sporadic errors like "Warning: array_merge_recursive(): recursion detected in /cake/libs/view/view.php on line 406".

The way to avoid such problems is to not do what isn't necessary ;)

// If the view already contains everything of 

interest to the Element there is no need to pass any params
echo $this->renderElement("Element");
// If there is some need 

to pass extra parameters, pass just the parameters of interest
echo 

$this->renderElement("Element",array("extra"=>$parameter));
  1. // If the view already contains everything of
  2. interest to the Element there is no need to pass any params
  3. echo $this->renderElement("Element");
  4. // If there is some need
  5. to pass extra parameters, pass just the parameters of interest
  6. echo
  7. $this->renderElement("Element",array("extra"=>$parameter));
  8.  

I hope the time I spent trying to figure this out is saved for others :)

«  »

    Comments are now closed, however feel free to send an email with your thoughts