Add the possibility to configure options that are passed to Haml::Engine when evalutating HAML template.

Options can be configured in two different ways:

* Application-wide, using set_option :haml
  e.g.: set_option :haml, :format      => :haml4,
                          :escape_html => true

* By passing options directly to the `haml` helper
  e.g.: haml '%strong Hello World', :options => {:format => :html4}

Note that if you use both way, options will be merged.
This commit is contained in:
Simon Rozet 2008-04-19 16:43:12 +02:00
parent d2ff40a7de
commit 23999054c6
2 changed files with 55 additions and 1 deletions

View File

@ -604,7 +604,8 @@ module Sinatra
private private
def render_haml(content, options = {}, &b) def render_haml(content, options = {}, &b)
::Haml::Engine.new(content).render(options[:scope] || self, options[:locals] || {}, &b) haml_options = (options[:options] || {}).merge(Sinatra.options.haml)
::Haml::Engine.new(content, haml_options).render(options[:scope] || self, options[:locals] || {}, &b)
end end
end end
@ -837,6 +838,7 @@ module Sinatra
:public => Dir.pwd + '/public', :public => Dir.pwd + '/public',
:sessions => false, :sessions => false,
:logging => true, :logging => true,
:haml => {}
} }
end end

View File

@ -178,4 +178,56 @@ context "Haml" do
end end
describe 'Options passed to the HAML interpreter' do
setup do
Sinatra.application = nil
end
specify 'are empty be default' do
get '/' do
haml 'foo'
end
Haml::Engine.expects(:new).with('foo', {}).returns(stub(:render => 'foo'))
get_it '/'
should.be.ok
end
specify 'can be configured by passing :options to haml' do
get '/' do
haml 'foo', :options => {:format => :html4}
end
Haml::Engine.expects(:new).with('foo', {:format => :html4}).returns(stub(:render => 'foo'))
get_it '/'
should.be.ok
end
specify 'can be configured using set_option :haml' do
configure do
set_option :haml, :format => :html4,
:escape_html => true
end
get '/' do
haml 'foo'
end
Haml::Engine.expects(:new).with('foo', {:format => :html4,
:escape_html => true}).returns(stub(:render => 'foo'))
get_it '/'
should.be.ok
end
end
end end