Gone are the days when you could just throw a few CSS definitions in something called ‘style.css’ and call it a good day’s work. With the arrival of semantic html where none of the styling is done inline it has become important to practice good coding habits while creating CSS styles.
It was embarrasingly recently that I had the habit of putting all my CSS into one file. I didn’t realize that it’s the same bad habit of making programs with only one (usually massive) file of source code. Learning to partition the code properly is a natural development step and it’s just as important for CSS as Ruby, Java, C, or any other.
How I do CSS: —-
What I’ve found is I have a few default CSS files named after the following pattern:
- application.css: usually will overwrite bad default values (like margins on forms)
- structure.css: the layout of the blocks that make up my layout (body, #wrap, #head, #main, #foot, etc.)
- fonts.css: font-family, font-size, etc. for all the popular styles
- forms.css: whether using label/input pairs or definition lists I need lots of styling for forms
- colors.css: super important to keep all color stuff together so you can both keep the colors in harmony and replace this file with some alternate color theme
These worked for a while but then I realized that I had little pieces of code that were needed that didn’t belong in any of these. I need to specify that a certain logo floats to the right or a users profile just happens to need a different background color.
The answer to this comes in using CSS files kinda like Rails uses partials. Some encapsulized code that has usefulness in a certain portion of your app. I did this at first by making stylesheets named after each of my controllers. This worked for about three seconds until I realized that I needed some conditional way to include them.
Conditionally including CSS files —- Including stylesheets conditionally turned out to be a fairly easy task. I’ll outline the solution here using extractions from my code:
1 2 3 4 5 6 |
class ApplicationController < ActionController::Base attr_accessor :stylesheets before_filter {|c| c.stylesheets ||= []; c.stylesheets << c.class.name.sub('Controller','').downcase } end |
This adds an instance var @stylesheets that starts as an array. It is given, by default the name of the currently active controller. note: this prints the derived class’s name, not ‘application’.
This means that you can now add stylesheets to the soon-to-be-rendered view anywhere in your code you want.
1 2 3 4 5 6 |
class UsersController < ApplicationController # if you have a stylesheet that governs just one action def show stylesheets << 'users_show' end end |
And it’s surprisingly simple to output the required code to the browser. Just add one line to your app/views/layouts/application.rhtml file:
1 2 3 4 5 6 7 |
<%= stylesheet_link_tag 'application', :media => 'all' %> <%= stylesheet_link_tag 'structure'', :media => 'all' %> <%= stylesheet_link_tag 'fonts'', :media => 'all' %> <%= stylesheet_link_tag 'forms', :media => 'all' %> <%= stylesheet_link_tag 'colors', :media => 'all' %> <%= @stylesheets.collect { |file| stylesheet_link_tag file }.join("\n") if @stylesheets %> <%= javascript_include_tag :defaults %> |
This is so simple that there’s no real point in turning it into a plugin but if anybody wants it that way just let me know.
Update: The version I ended up using in my app did some testing for the existence of the file before it included it as a stylesheet. I decided it was more important to avoid the 404 errors (especially the way they cruft up the logfile) than it was to have the app disk-optimized. So here’s my new application_controller:
1 2 3 4 5 6 7 8 9 10 |
class ApplicationController < ActionController::Base attr_accessor :stylesheets before_filter do |c| c.stylesheets ||= [] klass = c.class.name.sub('Controller','').downcase c.stylesheets << klass if File.exists?("#{RAILS_ROOT}/public/stylesheets/#{klass}.css") end end |
12 responses so far ↓
1 Ben Kittrell // Aug 21, 2006 at 12:43 AM
2 jason // Aug 21, 2006 at 01:53 AM
3 Danger // Aug 21, 2006 at 02:58 AM
4 Ben Kittrell // Aug 21, 2006 at 04:49 AM
5 Ashley Graham // Sep 13, 2006 at 07:41 AM
6 Ashley Graham // Sep 13, 2006 at 07:41 AM
7 Daniel Azuma // Nov 08, 2006 at 01:58 AM
8 John // Jan 29, 2007 at 02:29 PM
9 Danger // Jan 30, 2007 at 08:53 AM
10 Christop // Feb 02, 2007 at 01:21 AM
11 female-ejaculaton062 // Jul 13, 2008 at 06:39 PM
Bonjour. fels naptha fels naptha fender jaguar baritone guitar fender jaguar baritone guitar On this site it is possible to find all that you need. Good day.
12 jib-jab451 // Jul 16, 2008 at 05:05 PM
Hi. 2008 all star game box score 2008 all star game box score brian green brian green On this site it is possible to find all that you need. Thank you.
Leave a Comment