CakePHP Asset Mapper: CakePHP, PHP

CakePHP Asset Mapper

Category: PHP & CakePHP Tags: CakePHP, PHP | Written on Dec 26, 2007

Released my CakePHP Asset Mapper. I've always wanted an easier way to control my JavaScript and CSS includes and this is the solution I came up with. Controls the entire web application's includes in one file!

Here is an example of a mapping rule:

PHP:
  1. $this->AssetRule->create();
  2. $this->AssetRule->compact->css = array('site','global');
  3. $this->AssetRule->compact->scripts = array('jquery', 'ui.datepicker');
  4. $this->AssetRule->runRule();

Accross the entire site, this would compact the site.css and global.css files together and run them through CSS Tidy. jQuery.js and ui.datepicker.js would be compacted together and then ran through JSMin.

Great, this simplifies a complex process of compacting and minimizing - while still giving you the freedom to include scripts normally. Compacting lowers the amount of http requests you website makes, increasing website performance. JSMin and CSS Tidy strip comments and whitespace leaving your files much smaller in size.

You can create a rule to include files in a specific controller and/or action with AssetRule->map and AssetRule->controller:

PHP:
  1. $this->AssetRule->create();
  2. $this->AssetRule->map->controller = 'posts';
  3. $this->AssetRule->map->action = 'admin_add';
  4. $this->AssetRule->compact->css = array('user');
  5. $this->AssetRule->runRule();

This would only include your 'user.css' file in the, 'posts' controller - with the action, 'admin_add'. The css file would be compacted with the others.

Some scripts like TinyMCE, include other files (so it can't be compacted with the others) and requires a codeblock to be initialized. This configuration satisfies TinyMCE:

PHP:
  1. $this->AssetRule->create();
  2. $this->AssetRule->map->action = 'admin_edit';
  3. $this->AssetRule->scripts = array('tiny_mce/tiny_mce');
  4. $this->AssetRule->codeblock = 'tinyMCE.init({
  5.    mode : "textareas",
  6.    theme : "advanced",
  7.    plugins : "media",
  8.    media_external_list_url : "media/list.js",
  9.    theme_advanced_buttons1 : "bold,italic,underline,separator,strikethrough,justifyleft,justifycenter,justifyright, justifyfull,bullist,numlist,undo,outdent,indent,redo,link,unlink",
  10.    theme_advanced_buttons2 : "",
  11.    theme_advanced_buttons3 : "",
  12.    theme_advanced_resizing : true,
  13.    theme_advanced_toolbar_location : "top",
  14.    theme_advanced_toolbar_align : "left",
  15.    theme_advanced_statusbar_location : "bottom",
  16.    extended_valid_elements : "a[name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]"
  17. });';
  18. $this->AssetRule->runRule();

Coming from a heavy UI developer background, I can afford to be incredibly anal when managing my CSS and JavaScript files. I hope this saves you time, frustration and makes your developer-life better.

Comments

#1. grEvenX on Jan 16, 2008
What if you want to include CSS files from other sources that your own, say the Ext javascript framework which references images in a particular path. How would you go abouts fixing that if you still want to pack it all together as a single CSS file?
#2. Marc http://marcgrabanski.com on Jan 18, 2008
Interesting idea. I'm sure it would not be too difficult. I will take note of it as a feature request. Thanks for the idea!
#3. William on Jul 31, 2008
I'm trying to use the Assets Mapper with 1.2.0.7296 RC2 and get and strange error.

The layouts/default.ctp is renderized without the header.. any ideias?
#4. Niluka http://stockit.biz/ on Aug 05, 2008
Hi there!
Ur concept is really good.tho im having problems adapting it to my custom javascript.Could u drop a few more hints on how I could use it to compress the JS on my site (example JS available on the home page)?
Hope we could chat on the cake IRC sometime.
Thanks alot!
Cheers!
#5. Marc Grabanski http://marcgrabanski.com on Aug 05, 2008
Sorry Niluka, I'm not sure what you mean about, "adapting your custom JavaScript".
#6. Stefanski on Sep 12, 2008
Thanks a lot, it's exactly what I need ... but ... I try to get it running on cake 1.2 rev. 7496 - works with debug=1, but when setting debug to 0, my layout template is not rendered, only the view template. therefore I don't see any html header and the script-includes are missing? does that sound familiar to you?
#7. vijay http://www.leafgrafica.com on Apr 13, 2009
Its really an interesting idea.Thanks
#8. volka 1 week, 3 days ago
Hey Marc!
Cool stuff! Works great so far!
Just one problem: When setting debug = 0 the <head> is missing.
What did I do wrong?

greetz, volka
#9. Marc Grabanski http://marcgrabanski.com 1 week, 3 days ago
Hey volka, I'm not sure... try downloading the latest code off github and see if that fixes the problem you've been having.

Leave a Comment

Other Reading - Categories