Permalink
Browse files

Merge app-level haml/sass options with call options [#184]

Ensures globally set Haml/Sass configurations can be overridden on
an individual call basis, while retaining the global values.

Renamed the #haml() :options key to :haml_options for
clarity and consistency with the #sass method.
  • Loading branch information...
1 parent 2d60f3a commit d359dc9c4d6646138a68399830b4532be2d05ede @kematzy kematzy committed with rtomayko Mar 15, 2009
Showing with 105 additions and 6 deletions.
  1. +25 −0 README.rdoc
  2. +8 −4 lib/sinatra/base.rb
  3. +29 −2 test/haml_test.rb
  4. +43 −0 test/sass_test.rb
View
@@ -124,6 +124,18 @@ The haml gem/library is required to render HAML templates:
Renders <tt>./views/index.haml</tt>.
+{Haml's options}[http://haml.hamptoncatlin.com/docs/rdoc/classes/Haml.html]
+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 :haml, {:format => :html5 } # default Haml format is :xhtml
+
+ get '/' do
+ haml :index, :haml_options => {:format => :html4 } # overridden
+ end
+
+
=== Erb Templates
get '/' do
@@ -154,6 +166,19 @@ The sass gem/library is required to render Sass templates:
Renders <tt>./views/stylesheet.sass</tt>.
+{Sass' options}[http://haml.hamptoncatlin.com/docs/rdoc/classes/Sass.html]
+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 :sass, {:style => :compact } # default Sass style is :nested
+
+ get '/stylesheet.css' do
+ content_type 'text/css', :charset => 'utf-8'
+ sass :stylesheet, :sass_options => {:style => :expanded } # overridden
+ end
+
+
=== Inline Templates
get '/' do
View
@@ -239,13 +239,17 @@ def erb(template, options={})
def haml(template, options={})
require 'haml' unless defined? ::Haml::Engine
- options[:options] ||= self.class.haml if self.class.respond_to? :haml
+ opts = options[:haml_options] || options.delete(:options) || {}
+ opts = self.class.haml.merge(opts) if self.class.respond_to?(:haml)
+ options[:haml_options] = opts
render :haml, template, options
end
def sass(template, options={}, &block)
require 'sass' unless defined? ::Sass::Engine
- options[:layout] = false
+ opts = options[:sass_options] || options.delete(:options) || {}
@sbfaulkner
sbfaulkner Mar 31, 2009 Member

actually, I just realized the real problem was that the attempt at backwards compatibility used :options instead of :sass at line 250

+ opts = self.class.sass.merge(opts) if self.class.respond_to?(:sass)
+ options.merge! :layout => false, :sass_options => opts
render :sass, template, options
end
@@ -316,12 +320,12 @@ def render_erb(template, data, options, &block)
end
def render_haml(template, data, options, &block)
- engine = ::Haml::Engine.new(data, options[:options] || {})
+ engine = ::Haml::Engine.new(data, options[:haml_options] || {})
@sbfaulkner
sbfaulkner Mar 31, 2009 Member

Perhaps a bit more backwards compatability (so that noone else experiences the wild goose chase that I just had)…

engine = ::Haml::Engine.new(data, options[:haml_options] || options[:options] || {})

engine.render(self, options[:locals] || {}, &block)
end
def render_sass(template, data, options, &block)
- engine = ::Sass::Engine.new(data, options[:sass] || {})
+ engine = ::Sass::Engine.new(data, options[:sass_options] || {})
@sbfaulkner
sbfaulkner Mar 31, 2009 Member

Likewise…

engine = ::Sass::Engine.new(data, options[:sass_options] options[:sass] || {})

engine.render
end
View
@@ -47,11 +47,20 @@ def haml_app(&block)
end
it "passes HAML options to the Haml engine" do
- haml_app {
- haml "!!!\n%h1 Hello World", :options => {:format => :html5}
+ mock_app {
+ get '/' do
+ haml "!!!\n%h1 Hello World", :haml_options => {:format => :html5}
+ end
+ get '/backwards_compatible' do
+ haml "!!!\n%h1 Hello World", :options => {:format => :html4}
+ end
}
+ get '/'
assert ok?
assert_equal "<!DOCTYPE html>\n<h1>Hello World</h1>\n", body
+ get '/backwards_compatible'
+ assert ok?
+ assert_match(/^<!DOCTYPE html PUBLIC (.*) HTML 4.01/, body)
end
it "passes default HAML options to the Haml engine" do
@@ -65,4 +74,22 @@ def haml_app(&block)
assert ok?
assert_equal "<!DOCTYPE html>\n<h1>Hello World</h1>\n", body
end
+
+ it "merges the default HAML options with the overrides and passes them to the Haml engine" do
+ mock_app {
+ set :haml, {:format => :html5, :attr_wrapper => '"'} # default HAML attr are <tag attr='single-quoted'>
+ get '/' do
+ haml "!!!\n%h1{:class => :header} Hello World"
+ end
+ get '/html4' do
+ haml "!!!\n%h1{:class => 'header'} Hello World", :haml_options => {:format => :html4}
+ end
+ }
+ get '/'
+ assert ok?
+ assert_equal "<!DOCTYPE html>\n<h1 class=\"header\">Hello World</h1>\n", body
+ get '/html4'
+ assert ok?
+ assert_match(/^<!DOCTYPE html PUBLIC (.*) HTML 4.01/, body)
+ end
end
View
@@ -33,4 +33,47 @@ def sass_app(&block)
}
assert_raise(Errno::ENOENT) { get('/') }
end
+
+ it "passes SASS options to the Sass engine" do
+ sass_app {
+ sass "#sass\n :background-color #FFF\n :color #000\n", :sass_options => {:style => :compact}
+ }
+ assert ok?
+ assert_equal "#sass { background-color: #FFF; color: #000; }\n", body
+ end
+
+ it "passes default SASS options to the Sass engine" do
+ mock_app {
+ set :sass, {:style => :compact } # default Sass style is :nested
+ get '/' do
+ sass "#sass\n :background-color #FFF\n :color #000\n"
+ end
+ }
+ get '/'
+ assert ok?
+ assert_equal "#sass { background-color: #FFF; color: #000; }\n", body
+ end
+
+ it "merges the default SASS options with the overrides and passes them to the Sass engine" do
+ mock_app {
+ set :sass, {:style => :compact, :attribute_syntax => :alternate } # default Sass attribute_syntax is :normal (with : in front)
+ get '/' do
+ sass "#sass\n background-color: #FFF\n color: #000\n"
+ end
+ get '/raised' do
+ sass "#sass\n :background-color #FFF\n :color #000\n", :sass_options => {:style => :expanded } # retains global attribute_syntax settings
+ end
+ get '/expanded_normal' do
+ sass "#sass\n :background-color #FFF\n :color #000\n", :sass_options => {:style => :expanded, :attribute_syntax => :normal }
+ end
+ }
+ get '/'
+ assert ok?
+ assert_equal "#sass { background-color: #FFF; color: #000; }\n", body
+ assert_raise(Sass::SyntaxError) { get('/raised') }
+ get '/expanded_normal'
+ assert ok?
+ assert_equal "#sass {\n background-color: #FFF;\n color: #000;\n}\n", body
+
+ end
end

2 comments on commit d359dc9

@Aupajo
Contributor
Aupajo commented on d359dc9 Mar 31, 2009

+1 to sbfaulkner, only throw in a deprecation warning.

@sbfaulkner
Member

pull request submitted for backwards compatibility…

http://github.com/sbfaulkner/sinatra/commit/1f3d5c05068cb94b025d296aef900cfe61b9f9c5

Please sign in to comment.