Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix weirdness with partials in ERB [#158]

  • Loading branch information...
commit 090c4d79c8d73eb33e1d5d27e8dd9fc16c2b54ee 1 parent b398c1c
@rtomayko rtomayko authored
Showing with 21 additions and 0 deletions.
  1. +2 −0  CHANGES
  2. +5 −0 lib/sinatra/base.rb
  3. +14 −0 test/erb_test.rb
View
2  CHANGES
@@ -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
5 lib/sinatra/base.rb
@@ -281,12 +281,17 @@ def template_path(engine, template, options={})
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
14 test/erb_test.rb
@@ -64,4 +64,18 @@ def is; "IS." end
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
Please sign in to comment.
Something went wrong with that request. Please try again.