diff --git a/README.rdoc b/README.rdoc index 762c5a94..44afa407 100644 --- a/README.rdoc +++ b/README.rdoc @@ -203,6 +203,32 @@ and overridden on an individual basis. sass :stylesheet, :style => :expanded # overridden end +=== Scss Templates + +The sass gem/library is required to render Scss templates: + + ## You'll need to require haml or sass in your app + require 'sass' + + get '/stylesheet.css' do + content_type 'text/css', :charset => 'utf-8' + scss :stylesheet + end + +Renders ./views/stylesheet.scss. + +{Scss' options}[http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#options] +can be set globally through Sinatra's configurations, +see {Options and Configurations}[http://www.sinatrarb.com/configuration.html], +and overridden on an individual basis. + + set :scss, {:style => :compact } # default Scss style is :nested + + get '/stylesheet.css' do + content_type 'text/css', :charset => 'utf-8' + scss :stylesheet, :style => :expanded # overridden + end + === Less Templates The less gem/library is required to render Less templates: diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index d2d66549..ea522125 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -317,6 +317,11 @@ module Sinatra render :sass, template, options, locals end + def scss(template, options={}, locals={}) + options[:layout] = false + render :scss, template, options, locals + end + def less(template, options={}, locals={}) options[:layout] = false render :less, template, options, locals diff --git a/lib/sinatra/tilt.rb b/lib/sinatra/tilt.rb index 58adbe29..fe05e584 100644 --- a/lib/sinatra/tilt.rb +++ b/lib/sinatra/tilt.rb @@ -514,6 +514,31 @@ module Tilt register 'sass', SassTemplate + # Scss template implementation. + # + # Scss templates do not support object scopes, locals, or yield. + class ScssTemplate < Template + def initialize_engine + return if defined? ::Sass::Engine + require_template_library 'sass' + end + + def prepare + @engine = ::Sass::Engine.new(data, scss_options) + end + + def evaluate(scope, locals, &block) + @output ||= @engine.render + end + + private + def scss_options + options.merge(:filename => eval_file, :line => line, :syntax => :scss) + end + end + register 'scss', ScssTemplate + + # Lessscss template implementation. See: # http://lesscss.org/ # diff --git a/sinatra.gemspec b/sinatra.gemspec index 754b3db3..6ee84a71 100644 --- a/sinatra.gemspec +++ b/sinatra.gemspec @@ -4,7 +4,7 @@ Gem::Specification.new do |s| s.name = 'sinatra' s.version = '1.0' - s.date = '2010-03-23' + s.date = '2010-08-29' s.description = "Classy web-development dressed in a DSL" s.summary = "Classy web-development dressed in a DSL" @@ -17,6 +17,7 @@ Gem::Specification.new do |s| AUTHORS CHANGES LICENSE + README.de.rdoc README.jp.rdoc README.rdoc Rakefile diff --git a/test/scss_test.rb b/test/scss_test.rb new file mode 100644 index 00000000..7fccb8f2 --- /dev/null +++ b/test/scss_test.rb @@ -0,0 +1,64 @@ +require File.dirname(__FILE__) + '/helper' + +begin +require 'sass' + +class ScssTest < Test::Unit::TestCase + def scss_app(&block) + mock_app { + set :views, File.dirname(__FILE__) + '/views' + get '/', &block + } + get '/' + end + + it 'renders inline Scss strings' do + scss_app { scss "#scss {\n background-color: white; }\n" } + assert ok? + assert_equal "#scss {\n background-color: white; }\n", body + end + + it 'renders .scss files in views path' do + scss_app { scss :hello } + assert ok? + assert_equal "#scss {\n background-color: white; }\n", body + end + + it 'ignores the layout option' do + scss_app { scss :hello, :layout => :layout2 } + assert ok? + assert_equal "#scss {\n background-color: white; }\n", body + end + + it "raises error if template not found" do + mock_app { + get('/') { scss :no_such_template } + } + assert_raise(Errno::ENOENT) { get('/') } + end + + it "passes scss options to the scss engine" do + scss_app { + scss "#scss {\n background-color: white;\n color: black\n}", + :style => :compact + } + assert ok? + assert_equal "#scss { background-color: white; color: black; }\n", body + end + + it "passes default scss options to the scss engine" do + mock_app { + set :scss, {:style => :compact} # default scss style is :nested + get '/' do + scss "#scss {\n background-color: white;\n color: black;\n}" + end + } + get '/' + assert ok? + assert_equal "#scss { background-color: white; color: black; }\n", body + end +end + +rescue + warn "#{$!.to_s}: skipping scss tests" +end diff --git a/test/views/hello.scss b/test/views/hello.scss new file mode 100644 index 00000000..87d1200f --- /dev/null +++ b/test/views/hello.scss @@ -0,0 +1,3 @@ +#scss { + background-color: white +} \ No newline at end of file