Skip to content

Commit

Permalink
New feature - media types in stylesheets (issue #1)
Browse files Browse the repository at this point in the history
  • Loading branch information
SFEley committed May 20, 2010
1 parent 9156614 commit a11f01f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.markdown
Expand Up @@ -114,6 +114,12 @@ Like the _title_ setting, the _stylesheets_ setting begins life as an empty arra
stylesheets << 'specific.css'
end

If your stylesheet string contains a space, the second and any following words will be used for the _media_ attribute:

stylesheets << 'no_effects.css print braille'
# yields:
<link rel='stylesheet' href='/stylesheets/no_effects.css' media='print, braille' />

In your layout, you can call the `stylesheet_tag` helper for a single filename or URL you provide, or the `stylesheet_tags` helper which walks the array and creates a tag for each. (In FIFO or queue order, unlike _title._)

Currently nothing is done for nicer asset packaging, fingerprinting, compressing, etc. There's [Rack][10] [middleware][9] for some of it, and future iterations may include these features. If you'd really really like to see them, [create an issue][7] and ask for them.
Expand Down
11 changes: 9 additions & 2 deletions lib/sinatra/head/tag_helpers.rb
Expand Up @@ -35,10 +35,17 @@ def title_tag

# Spits out a <link rel='stylesheet'> element for the given stylesheet reference.
# Relative filenames will be expanded with the Sinatra assets path, if set.
# If the filename string contains multiple words (separated by spaces), all
# words after the first will be used for _media_ identifiers.
#
# @param [String] sheet
def stylesheet_tag(sheet)
"<link rel='stylesheet' href='#{expand_stylesheet_path(sheet)}' />"
def stylesheet_tag(reference)
sheet, *media = reference.split
if media.empty?
"<link rel='stylesheet' href='#{expand_stylesheet_path(sheet)}' />"
else
"<link rel='stylesheet' href='#{expand_stylesheet_path(sheet)}' media='#{media.join(', ')}' />"
end
end

# Spits out stylesheet tags for all declared stylesheets, one per line.
Expand Down
4 changes: 4 additions & 0 deletions spec/sinatra/stylesheet_spec.rb
Expand Up @@ -45,6 +45,10 @@ class DummyFixture::DummyChild
it "knows how to make a tag" do
@instance.stylesheet_tag(@instance.stylesheets.first).should == "<link rel='stylesheet' href='/stuff/stylesheets/main.css' />"
end

it "can make a stylesheet tag just for a specific medium" do
@instance.stylesheet_tag('print.css print braille').should == "<link rel='stylesheet' href='/stuff/stylesheets/print.css' media='print, braille' />"
end

it "knows how to make all the tags" do
@instance.stylesheets << 'http://yahoo.com/yahoo_style.css'
Expand Down

0 comments on commit a11f01f

Please sign in to comment.