Code
CakePHP Asset Helper Updated
I've been using this helper on some projects lately and ran into a few issues, plus there have been some good comments, so I figured it could use a few tweaks. The new version is available for download on the Sandbox page.
Change Log
- Removed deprecated DEBUG, using Configure::read('debug') instead.
- Strip "." from file names. This seemed to be causing problems with IE7.
- Option to MD5 outputted file name.
- Debug flag.
- Packed files now have seperator lines.
- Improved .htaccess included. Based on the comment by Marcin Domanski on the CakePHP Bakery.
Automatic JavaScript and CSS Packer Helper for CakePHP
Download
http://github.com/mcurry/cakephp/tree/master/helpers/asset
The Story
A little while ago I wrote a post about how I manually combine/pack Javascript files. Even as I was writing it I was thinking "hey, this would be a great CakePHP plugin/component/helper. Before I could code it Brad Daily beat me to it. However there are a couple of things I didn't like about his method, particularly the need for configuration files (how anti Cake!) and the need to run a console script to generated the packed versions.
My First Solution
Suitably inspired I whipped up my own version. And it was awesome. It was so friggen automagic that you didn't even have to change your code. Just include the helper and it did the rest - assuming you were using the new (as in Cake 1.2) scripts_for_layout syntax and not adding your scripts inline.
The Snag
Unfortunately I had missed a small issue. Since the helper relied on the afterRender callback to do most of the work any js/css added in the layout was missed. See the afterRender is called after the view is rendered, but BEFORE the layout is rendered. Doh! Digging through the base helper I saw a promising callback, afterLayout. After a bit more digging I found that this is merely a placeholder (added sometime around new years) and isn't actually called anywhere. I'm not sure if it would have worked in my case anyway, since I really needed something like duringLayoutHeaderRender callback.
My Second Solution
So I reworked the interface of the helper. It's less automagic now, but works with scripts from both the layout and view. Basically instead of just echoing $scripts_for_layout you use the helper. Otherwise you still include JavaScript and CSS as normal, making sure to set inline to false. Full directions and an example are in the Sandbox as usual.
Pros and Cons
I like Brad's helper better for certain projects. Particularly ones that uses a large amount of JavaScript, as his photo manager app does. But I think for most small sites, with just a few files the automatic approach works out better. Plus mine packs CSS files in addition to JS. This is probably a good time to mention that Brad's app, SlideShowPro, is totally awesome. I recently used it when developing AndrewSaul.com. Basically I went from no photo gallery to full featured, cool looking, seamlessly integrated photo gallery in thirty minutes. Seriously, I was shocked when I read this was the product of just two guys working nights.
Bringing It Home
Checkout the helper. It's kinda cool. The Sandbox jumped from a 64 to a 79 in YSlow after I applied it. I also included an .htaccess file that handles things like gzip, expire time and ETAGS. Unfortunately my host doesn't support any of those, so I didn't get any benefit from it.
SimplePie CakePHP Component
I'm working on a new design for my personal site, mcurry.net, part of which is basing the site on CakePHP. Although Cake is probably a little overkill in this situation, I've taken to using Cake for pretty much any site. The pretty urls alone make it worthwhile. And sites that start out as a single static page have a way of growing.
Part of the site is going to show the feed for this blog. SimplePie is the slickest PHP class for parsing RSS feeds. Using the class with Cake's vendor system was an easy call. While I was on the SimplePie site I came across a this component, which unfortunately wasn't up to date with the latest version of SimplePie.
I've updated the component to work with the SimplePie 1.0.1 and CakePHP 1.1 and 1.2. The updated SimplePie CakePHP component, along with instructions are now available.
CakePHP Automatic Layout Switcher
On my old blog I kept a subdomain that I called the "sandbox". It was a standard CakePHP installation, where I could post plugins or code that I intended to release. Since the old blog is deprecated, I wanted to port the sandbox to this blog, basically give it a design that matched. This is normally pretty straightforward, so to up the degree of difficulty I decided to keep the old design as well and automatically switch the layout based the domain.
To accomplish this I created a simple layout switcher component that determined the domain and uses that information to select the correct layout. All you have to do is include it in your controller and it will automatically do the rest.
Most likely you want to apply it to your whole site, so you should put it in your app_controller. Full instructions and downloads are available on the sandbox
Now if you go to sandbox.pseudocoder.com you'll see a different layout then sandbox.siteamonth.com.
