mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
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
2 changed files with 20 additions and 2 deletions
|
@ -483,17 +483,18 @@ module Sinatra
|
|||
layout = @default_layout if layout.nil? or layout == true
|
||||
content_type = options.delete(:content_type) || options.delete(:default_content_type)
|
||||
layout_engine = options.delete(:layout_engine) || engine
|
||||
scope = options.delete(:scope) || self
|
||||
|
||||
# compile and render template
|
||||
layout_was = @default_layout
|
||||
@default_layout = false
|
||||
template = compile_template(engine, data, options, views)
|
||||
output = template.render(self, locals, &block)
|
||||
output = template.render(scope, locals, &block)
|
||||
@default_layout = layout_was
|
||||
|
||||
# render 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 }}
|
||||
end
|
||||
|
||||
|
|
|
@ -229,6 +229,23 @@ class TemplatesTest < Test::Unit::TestCase
|
|||
render_app { render :erb, :calc }
|
||||
assert_equal '2', body
|
||||
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__ : this is not the real end of the script.
|
||||
|
|
Loading…
Reference in a new issue