CakePHP Asset Packer Helper (v1.2) Question/Comments
Notes
- JavaScript packing only works with PHP5. If you're using PHP4 the scripts will still be merged into one file, but not packed.
- A sample .htaccess file is included in the zip. Rename it and drop it in js/packed and css/packed for improved performance.
Example
Do a view source on this page and you'll see the filenames for the packed CSS and JS. They'll be something like "/(css|js)/packed/*some long string*_*10 digit timestamp*.(css|js)"
ReadMe
- You'll need a working version of CakePHP installed. This is running on 1.2.0.6311 beta.
- Download jsmin 1.1.0 or later and put it in vendors/jsmin.
- Download CSSTidy 1.3 or later and put the contents in vendors/csstidy.
- Download the helper and unzip it to app/views/helpers.
- Include the helper in any controller that will need it. Most likely you will put it in AppController so that it's available to all your controllers:
var $helpers = array('Asset'); -
In your layout and views include Javascript files as you normally would when using the $scripts_for_layout approach:
$javascript->link('jquery', false); -
Similarly include your css with the inline option set to false:
$html->css('style', null, null, false); -
Then in your layout file, in the head section, instead of using $scripts_for_layout call the helper:
echo $asset->scripts_for_layout();
Tips
- Remember to set the inline option to false for JS and CSS in your layout if you want them to be packed with the view scripts.
- Setting DEBUG on will cause this helper to output the scripts the same way $scripts_for_layout would, effectifly turning it off while testing.
- If you get a JavaScript error with a packed version of a file it's most likely missing a semi-colon somewhere.
- Order is important. If you include script1 then script2 on one view and script2 then script1 on another, they will generate separate packed versions and will be treated by the browser as separate scripts.
Change Log
- 1.1 - New debug syntax. Option to MD5 outputted file name. Debug flag. Packed files now have seperator lines. Improved .htaccess included.
