I'm working on a updated web application for a friend; built atop the CakePHP (v1.2) framework. I'm still very new to CakePHP, which is a "Rails-inspired" frameworks (one that follows convention over configuration), but like it a great deal.

I ran into a problem (sort of) tonight: I could not get the JavaScript helper's link method to output the script tag for my jQuery library, as it "should".

<?php echo $javascript->link( 'jquery/jquery', false ); ?>

This expression should output the following in the head section of my HTML:


It didn't. I read in the API/Docs and verified that this method ($javascript->link( 'tofile', false )), when called from any view, is intended to output the script tag where ever the following code is located:

<?php echo $scripts_for_layout; ?>

If I changed the second parameter from false to true, the expression output the script tag inline rather than at the location of echo $scripts_for_layout.

After further digging and experimentation, I realized I was not executing the above expression from a view. It was from a particularly handy feature of Cake when working with views: elements.

What threw me is that elements are located within the view, in terms of file structure and such. So, I assumed all elements were view templates and would act accordingly. They aren't! Long story short: if you're working in an element with CakePHP, remember they are not the same as view templates :)!

Comments