Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tag: 1.3.0
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 151 lines (102 sloc) 5.259 kb

Rack::Less

Description

A better way to use LESS CSS in Ruby web apps.

Installation

$ gem install rack-less

Basic Usage

Rack::Less is implemented as a piece of Rack middleware and can be used with any Rack-based application. If your application includes a rackup (`.ru`) file or uses Rack::Builder to construct the application pipeline, simply require and use as follows:

require 'rack/less'

# optional - use as necessary
Rack::Less.configure do |config|
  config.compress = true
  # other configs ...
end

use Rack::Less,
  :source    => 'app/less',
  :hosted_at => '/'
  # additional options ...

run app

Using with Rails

Add this to your `config/environment.rb`:

config.middleware.use "Rack::Less"

Add any configs in an initializer (optional - use as necessary):

Rack::Less.configure do |config|
  config.cache = true
  # additional configs ...
end

You should now see `Rack::Less` listed in the middleware pipeline:

rake middleware

Available Options

  • :root [“.”]

    • The app root. The reference point for the source and public options.

  • :source ['app/stylesheets']

    • The path (relative to the root) where LESS source files are located

  • :public ['public']

    • The path where static files are located

  • :hosted_at ['/stylesheets']

    • The public HTTP path for hosted stylesheets.

Available Configurations

  • .cache [false]

    • Whether to cache the compilation output to a corresponding static file

  • .compress [false]

    • Whether or not to apply compression to the concatenation output

      • :yui - use YUI Compressor (gem install yui-compressor)

      • :whitespace - remove extraneous whitespace only.

  • .combinations [{}]

    • Directives for combining the output of many stylesheets and serving them as a single resource.

  • .combination_timestamp [false]

    • Directives for timestamping (cache-busting) while using combinations

      • :false - don't explicitly add timestamps

      • :true - add Time.now.to_i as the explicit timestamp (will never cache)

      • :*<any other value>* - add the value as “foo.css?<value>” (use something like a File.mtime to cache-bust like Rails does)

Combinations

At times, it is useful to combine multiple stylesheets and serve them as one resource. For example you may have two sets of stylesheets: one for traditional web views and one for mobile web views. Rails' provides the :cache option on 'stylesheet_link_tag' helper to provide this function, ie:

stylesheet_link_tag 'reset', 'common', 'app_web', :cache => 'web'
stylesheet_link_tag 'reset', 'common', 'iui', 'app_mobile', :cache => 'mobile'

Rack::Less uses combinations, in conjunction with the :cache config setting, to provide this function. Combinations are configured using a hash, where the key is the resource name and the value is an array of names corresponding to stylesheets to combine as the named resource. For the above example, use a configuration like this:

Rack::Less.configure do |config|
  config.combinations = {
    'web' => ['reset', 'common', 'app_web'],
    'mobile' => ['reset', 'common', 'iui', 'app_mobile']
  }
end

and stylesheet link tags like this, respectively:

# equivalent to: stylesheet_link_tag 'reset', 'common', 'app_web'
stylesheet_link_tag Rack::Less.combinations('web')

# equivalent to: stylesheet_link_tag 'reset', 'common', 'iui', 'app_mobile'
stylesheet_link_tag Rack::Less.combinations('mobile')

If you configure Rack::Less to cache, with something like this:

Rack::Less.config.cache = true

then the same stylesheet link tags behave like they have the :cache option set, respectively:

# equivalent to: stylesheet_link_tag 'reset', 'common', 'app_web', :cache => 'web'
stylesheet_link_tag Rack::Less.combinations('web')

# equivalent to: stylesheet_link_tag 'reset', 'common', 'iui', 'app_mobile', :cache => 'mobile'
stylesheet_link_tag Rack::Less.combinations('mobile')

Links

License

Copyright © 2010 Kelly Redding (kelly@kelredd.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Something went wrong with that request. Please try again.