public Method

AssetTagHelper.stylesheet_link_tag(*sources)

Returns a stylesheet link tag for the sources specified as arguments. If you don’t specify an extension, .css will be appended automatically. You can modify the link attributes by passing a hash as the last argument.

Examples

stylesheet_link_tag "style" # =>
  <link href="/stylesheets/style.css" rel="stylesheet" media="screen" type="text/css" />

stylesheet_link_tag "style.css" # =>
  <link href="/stylesheets/style.css" rel="stylesheet" media="screen" type="text/css" />

stylesheet_link_tag "http://www.railsapplication.com/style.css" # =>
  <link href="http://www.railsapplication.com/style.css" rel="stylesheet" media="screen" type="text/css" />

stylesheet_link_tag "style", :media => "all" # =>
  <link href="/stylesheets/style.css" rel="stylesheet" media="all" type="text/css" />

stylesheet_link_tag "style", :media => "print" # =>
  <link href="/stylesheets/style.css" rel="stylesheet" media="print" type="text/css" />

stylesheet_link_tag "random.styles", "/css/stylish" # =>
  <link href="/stylesheets/random.styles" rel="stylesheet" media="screen" type="text/css" />
  <link href="/css/stylish.css" rel="stylesheet" media="screen" type="text/css" />

You can also include all styles in the stylesheet directory using :all as the source:

stylesheet_link_tag :all # =>
  <link href="/stylesheets/style1.css" rel="stylesheet" media="screen" type="text/css" />
  <link href="/stylesheets/styleB.css" rel="stylesheet" media="screen" type="text/css" />
  <link href="/stylesheets/styleX2.css" rel="stylesheet" media="screen" type="text/css" />

Caching multiple stylesheets into one

You can also cache multiple stylesheets into one file, which requires less HTTP connections and can better be compressed by gzip (leading to faster transfers). Caching will only happen if ActionController::Base.perform_caching is set to true (which is the case by default for the Rails production environment, but not for the development environment). Examples:

Examples

stylesheet_link_tag :all, :cache => true # when ActionController::Base.perform_caching is false =>
  <link href="/stylesheets/style1.css" rel="stylesheet" media="screen" type="text/css" />
  <link href="/stylesheets/styleB.css" rel="stylesheet" media="screen" type="text/css" />
  <link href="/stylesheets/styleX2.css" rel="stylesheet" media="screen" type="text/css" />

stylesheet_link_tag :all, :cache => true # when ActionController::Base.perform_caching is true =>
  <link href="/stylesheets/all.css" rel="stylesheet" media="screen" type="text/css" />

stylesheet_link_tag "shop", "cart", "checkout", :cache => "payment" # when ActionController::Base.perform_caching is false =>
  <link href="/stylesheets/shop.css" rel="stylesheet" media="screen" type="text/css" />
  <link href="/stylesheets/cart.css" rel="stylesheet" media="screen" type="text/css" />
  <link href="/stylesheets/checkout.css" rel="stylesheet" media="screen" type="text/css" />

stylesheet_link_tag "shop", "cart", "checkout", :cache => "payment" # when ActionController::Base.perform_caching is true =>
  <link href="/stylesheets/payment.css" rel="stylesheet" media="screen" type="text/css" />

Source Code

# File action_view/helpers/asset_tag_helper.rb, line 334
def stylesheet_link_tag(*sources)
  options = sources.extract_options!.stringify_keys
  cache   = options.delete("cache")

  if ActionController::Base.perform_caching && cache
    joined_stylesheet_name = (cache == true ? "all" : cache) + ".css"
    joined_stylesheet_path = File.join(STYLESHEETS_DIR, joined_stylesheet_name)

    write_asset_file_contents(joined_stylesheet_path, compute_stylesheet_paths(sources))
    stylesheet_tag(joined_stylesheet_name, options)
  else
    expand_stylesheet_sources(sources).collect { |source| stylesheet_tag(source, options) }.join("\n")
  end
end
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.