mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
Allow helper methods to use blocks in erb views
This commit is contained in:
parent
004468370b
commit
f3ed6e4dd9
2 changed files with 18 additions and 1 deletions
|
@ -282,7 +282,7 @@ module Sinatra
|
|||
|
||||
def render_erb(template, data, options, &block)
|
||||
data = data.call if data.kind_of? Proc
|
||||
instance = ::ERB.new(data)
|
||||
instance = ::ERB.new(data, nil, nil, '@_out_buf')
|
||||
locals = options[:locals] || {}
|
||||
locals_assigns = locals.to_a.collect { |k,v| "#{k} = locals[:#{k}]" }
|
||||
src = "#{locals_assigns.join("\n")}\n#{instance.src}"
|
||||
|
|
|
@ -47,4 +47,21 @@ describe "ERB Templates" do
|
|||
assert ok?
|
||||
assert_equal "ERB Layout!\nHello World\n", body
|
||||
end
|
||||
|
||||
it "renders erb with blocks" do
|
||||
mock_app {
|
||||
def container
|
||||
@_out_buf << "THIS."
|
||||
yield
|
||||
@_out_buf << "SPARTA!"
|
||||
end
|
||||
def is; "IS." end
|
||||
get '/' do
|
||||
erb '<% container do %> <%= is %> <% end %>'
|
||||
end
|
||||
}
|
||||
get '/'
|
||||
assert ok?
|
||||
assert_equal 'THIS. IS. SPARTA!', body
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue