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.
This commit is contained in:
parent
2d60f3ad94
commit
d359dc9c4d
25
README.rdoc
25
README.rdoc
|
@ -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
|
||||
|
|
|
@ -239,13 +239,17 @@ module Sinatra
|
|||
|
||||
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) || {}
|
||||
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 @@ module Sinatra
|
|||
end
|
||||
|
||||
def render_haml(template, data, options, &block)
|
||||
engine = ::Haml::Engine.new(data, options[:options] || {})
|
||||
engine = ::Haml::Engine.new(data, options[:haml_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] || {})
|
||||
engine.render
|
||||
end
|
||||
|
||||
|
|
|
@ -47,11 +47,20 @@ describe "HAML Templates" do
|
|||
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 @@ describe "HAML Templates" do
|
|||
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
|
||||
|
|
|
@ -33,4 +33,47 @@ describe "Sass Templates" do
|
|||
}
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue