Fix weirdness with partials in ERB [#158]

This commit is contained in:
Ryan Tomayko 2009-02-21 23:16:53 -08:00
parent b398c1c83b
commit 090c4d79c8
3 changed files with 21 additions and 0 deletions

View File

@ -18,6 +18,8 @@
* Fix :provides causing crash on any request when request has no
Accept header [#139]
* Fix that ERB templates were evaluated twice per "erb" call.
* The ERB output buffer is now available to helpers via the @_out_buf
instance variable.
= 0.9.0.4 / 2009-01-25

View File

@ -281,12 +281,17 @@ module Sinatra
end
def render_erb(template, data, options, &block)
original_out_buf = @_out_buf
data = data.call if data.kind_of? Proc
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}"
eval src, binding, '(__ERB__)', locals_assigns.length + 1
@_out_buf, result = original_out_buf, @_out_buf
result
end
def render_haml(template, data, options, &block)

View File

@ -64,4 +64,18 @@ describe "ERB Templates" do
assert ok?
assert_equal 'THIS. IS. SPARTA!', body
end
it "can be used in a nested fashion for partials and whatnot" do
mock_app {
template(:inner) { "<inner><%= 'hi' %></inner>" }
template(:outer) { "<outer><%= erb :inner %></outer>" }
get '/' do
erb :outer
end
}
get '/'
assert ok?
assert_equal '<outer><inner>hi</inner></outer>', body
end
end