CSS Server-side Constants for Ruby/Rails
RCSS implements
CSS-SSC in Ruby.
Main features:
* Create rich
CSS files using:
o ERB templates
o Server-side constants
o Server-side classes (preliminary)
* Simply add
RCSS files to any Rails application
* Process
RCSS files off-line using command line executable
แนะนำ
ช่วยในการสร้าง Dynamic
CSS stylesheets in Rails
RCSS addresses issue of
CSS maintainability. Even a minor change in complex stylesheet usually requires using search-and-replace. A simple way to fix this flaw is to allow some kind of constant/variable substitution inside
CSS. Using
RCSS you can achieve that in three ways:
ERB templates
ERB templating is the most flexible solution. It uses standard ERB syntax (the same you can find in rhtml files). ERB allows you to inlude in the
CSS values defined in controller. This gives access to database through
ActiveRecord? and allows database-driven
CSS themes.
Strengths
* Access to application data (through
ActiveRecord?)
* Flexible Ruby syntax (allowing programmatic
CSS)
Weaknesses
* Syntax unfamiliar to
CSS designers
* A bit ugly and sometimes error-prone embedding
* Not portable
Example
<% template_color = "#000"
template_background = "#fff"
template_highlight = "#fa3"
%>
body {
color: <%= template_color %>;
background-color: <%= template_background %>;
}
h1 {
color: <%= template_background %>;
background-color: <%= template_highlight %>;
}
Server-side Constants
Server-side Constants were originaly developed by Shaun Inman in PHP. SSC extends
CSS syntax in a simple way. SCC is cross-platform and easy to use.
RCSS differs from original implementation in two ways :
* Constants may have complex values (including spaces and commas)
* Includes are not implemented
Strengths
* Simple syntax
* SSC-CSS files can be used in both Rails and PHP applications
Weaknesses
* Only simple constant substitution
ตัวอย่างการเขียน code
Controller
class StylesheetsController < ApplicationController
layout nil
session :off
def rcss
if rcss = params[:rcss]
file_base = rcss.gsub(/\.css$/i, '')
file_path = "#{RAILS_ROOT}/app/views/stylesheets/#{file_base}.rcss"
@color = '#f77' # example setting
render(:file => file_path, :content_type => "text/css")
else
render(:nothing => true, :status => 404)
end
end
end
การติดตั้ง
แบบ
RubyGems
ติดตั้งโดยใช้คำสั่งด้านล่างนี้
gem install --remote rcss
ระบบจะดาวโหลดและติดตั้ง component โดยอัตโนมัติ
แบบ Manual
ดาวโหลดไฟล์จาก
RCSS project page: rubyforge.org/projects/rcss และติดตั้งโดยใช้คำสั่งด้านล่างนี้
# ruby install.rb
อ้างอิง