Skip to content

Commit

Permalink
Fix weirdness with partials in ERB [sinatra#158]
Browse files Browse the repository at this point in the history
  • Loading branch information
rtomayko committed Feb 22, 2009
1 parent b398c1c commit 090c4d7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Expand Up @@ -18,6 +18,8 @@
* Fix :provides causing crash on any request when request has no * Fix :provides causing crash on any request when request has no
Accept header [#139] Accept header [#139]
* Fix that ERB templates were evaluated twice per "erb" call. * 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 = 0.9.0.4 / 2009-01-25


Expand Down
5 changes: 5 additions & 0 deletions lib/sinatra/base.rb
Expand Up @@ -281,12 +281,17 @@ def template_path(engine, template, options={})
end end


def render_erb(template, data, options, &block) def render_erb(template, data, options, &block)
original_out_buf = @_out_buf
data = data.call if data.kind_of? Proc data = data.call if data.kind_of? Proc

instance = ::ERB.new(data, nil, nil, '@_out_buf') instance = ::ERB.new(data, nil, nil, '@_out_buf')
locals = options[:locals] || {} locals = options[:locals] || {}
locals_assigns = locals.to_a.collect { |k,v| "#{k} = locals[:#{k}]" } locals_assigns = locals.to_a.collect { |k,v| "#{k} = locals[:#{k}]" }

src = "#{locals_assigns.join("\n")}\n#{instance.src}" src = "#{locals_assigns.join("\n")}\n#{instance.src}"
eval src, binding, '(__ERB__)', locals_assigns.length + 1 eval src, binding, '(__ERB__)', locals_assigns.length + 1
@_out_buf, result = original_out_buf, @_out_buf
result
end end


def render_haml(template, data, options, &block) def render_haml(template, data, options, &block)
Expand Down
14 changes: 14 additions & 0 deletions test/erb_test.rb
Expand Up @@ -64,4 +64,18 @@ def is; "IS." end
assert ok? assert ok?
assert_equal 'THIS. IS. SPARTA!', body assert_equal 'THIS. IS. SPARTA!', body
end 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 end

0 comments on commit 090c4d7

Please sign in to comment.