new template rendering option: :scope - eval in a scope other than self
Signed-off-by: Konstantin Haase <konstantin.mailinglists@googlemail.com>
This commit is contained in:
parent
9db223ea34
commit
8c00771486
|
@ -483,17 +483,18 @@ module Sinatra
|
||||||
layout = @default_layout if layout.nil? or layout == true
|
layout = @default_layout if layout.nil? or layout == true
|
||||||
content_type = options.delete(:content_type) || options.delete(:default_content_type)
|
content_type = options.delete(:content_type) || options.delete(:default_content_type)
|
||||||
layout_engine = options.delete(:layout_engine) || engine
|
layout_engine = options.delete(:layout_engine) || engine
|
||||||
|
scope = options.delete(:scope) || self
|
||||||
|
|
||||||
# compile and render template
|
# compile and render template
|
||||||
layout_was = @default_layout
|
layout_was = @default_layout
|
||||||
@default_layout = false
|
@default_layout = false
|
||||||
template = compile_template(engine, data, options, views)
|
template = compile_template(engine, data, options, views)
|
||||||
output = template.render(self, locals, &block)
|
output = template.render(scope, locals, &block)
|
||||||
@default_layout = layout_was
|
@default_layout = layout_was
|
||||||
|
|
||||||
# render layout
|
# render layout
|
||||||
if layout
|
if layout
|
||||||
options = options.merge(:views => views, :layout => false, :eat_errors => eat_errors)
|
options = options.merge(:views => views, :layout => false, :eat_errors => eat_errors, :scope => scope)
|
||||||
catch(:layout_missing) { output = render(layout_engine, layout, options, locals) { output }}
|
catch(:layout_missing) { output = render(layout_engine, layout, options, locals) { output }}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -229,6 +229,23 @@ class TemplatesTest < Test::Unit::TestCase
|
||||||
render_app { render :erb, :calc }
|
render_app { render :erb, :calc }
|
||||||
assert_equal '2', body
|
assert_equal '2', body
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "passes scope to the template" do
|
||||||
|
mock_app {
|
||||||
|
template :scoped do
|
||||||
|
'Hello <%= foo %>'
|
||||||
|
end
|
||||||
|
|
||||||
|
get '/' do
|
||||||
|
some_scope = Class.new; def foo; 'World!'; end;
|
||||||
|
erb :scoped, :scope => some_scope
|
||||||
|
end
|
||||||
|
}
|
||||||
|
|
||||||
|
get '/'
|
||||||
|
assert ok?
|
||||||
|
assert_equal 'Hello World!', body
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# __END__ : this is not the real end of the script.
|
# __END__ : this is not the real end of the script.
|
||||||
|
|
Loading…
Reference in New Issue