Jeroen Wellner

Mobile & Web Development

CakePHP javascript->link in elements

August 18, 2009

For my CakePHP project I wanted to use the Javascript helper in a element. This element needs a javascript include (which i only use for this element) so I thought to it would be a good place to add it using the Javascript helper in the element template. To my surprise the javascript did not end up in the $scripts_for_layout variable. After hours of searching and debugging I found issue #6323 in CakePHP trac :

scripts_for_layout is generated _before_ the layout is rendered. Therefore it cannot contain scripts from elements that are included in the layout as that would require bending of space time. As templates are just plain PHP, and are parsed by the PHP interpreter only.

There are alternative solutions for this in the works, but at this time rebuilding space time is a too large of a task. :)

I think the CakePHP team will find some solution for this. Before that you can use the solution_ therma_lobsterdore_ provided:

You can get around this problem by manually recreating scripts_for_layout. Remove $scripts_for_layout from your view and replace it with this...

echo join("\n\t", $this->__scripts);

I hope this helps some people experiencing the same problem :)