I’ve come to the conclusion that I am outright fed up with CSS. Organizing it, upkeep of it, repeating the same thing over and over, all of it sucks. What makes matters worse is that everyone has their way of doing it “properly” — there are 1,650,000 pages that popup for a search of “how to organize css” — that’s insane. Jeff Atwood puts it a bit more concisely…

In short, CSS violates the living crap out of the DRY principle. You are constantly and unavoidably repeating yourself.

Because of the sheer amount of suck this causes so many people there are a ton of fairly nifty tools out there for mitigating CSS’ short comings. One of which is called Sass which boasts nested rules, variables, and mixins. I love me some mixins.

Despite being a Ruby gem, some good folks have ported the logic to PHP and made PHamlP. Now, I’m way too lazy to just run their code every time I need to make a new CSS from Sass so, I whipped together Sassy, a CakePHP 1.3+ plugin to handle all that for me.

It really does nothing more than parsing the Sass files if they’re newer than their CSS counterpart. Really. That’s it. You can still use any asset packer you like, you don’t have to change the way you include CSS files — you just have to add one line to your AppController.

If you felt so inclined though, there are a couple configuration options you have available to you:

// Look for files in /path/app/webroot/css/sass and save them to /path/app/webroot/css
Configure::write('Sassy.Recompile.Folders', array(
    '/path/app/webroot/css/sass' => '/path/app/webroot/css',
    '/path/app/webroot/multiple/folders' => '/path/app/webroot/are/awesome'
));

// Make Sassy check for updates on every other request (statistically)
Configure::write('Sassy.Recompile.Percentage', 50);

// If we get this named parameter in the URL, lets force a check for updates
Configure::write('Sassy.Recompile.Parameter', 'recompile');

The plugin is, at the time of writing this, still fairly new so there’s a pretty good chance you may run into an issue with it. As always, if you find a problem or want to make it better you should feel free to fork the project and help out.

  • This is cool to see, but (as the lead developer of the Ruby version of Sass) I’d strongly recommend using the Ruby gem rather than the PHamlP port. It has a full command-line interface that should be easy to call out to from PHP.

    The reason for this is that Sass is a reasonably complex language and moves very quickly, and it’s hard for ports to keep pace. For instance, PHamlP has no support for any of the improvements in the Sass 3, and it has several subtle incompatibilities with Sass 2.2. In the interest of both consistency of user experience and compatibility with libraries such as Compass (http://compass-style.org), I believe it’s better to hook into the Ruby version.

  • @Nathan
    Thanks for the input. Actually I did see the CLI version and while I tossed around the idea of making that a prerequisite for the plugin, requiring an entire library for the sake of one program was not something I could choke down.

    While I agree that it probably would be better for the functionality itself I feel the quirks and possible shortcomings of the PHP version is an acceptable middle-ground.

  • This all sounds very cool; indeed I love using compass/sass/scss but being more of a php user specifically cakephp this sound truly fantastic

    totally agree with the finding middle ground point.

    Have you thought about making this work as a filter for Mark Story’s Asset Compress

    (some of the above is perhaps pre-emptive since i have not played with this as yet)

  • Actually you should still be able to use it with any asset packer. I can’t say with certainty though since I’m not familiar with Mark Story’s asset compress.

Comment on this post