Skip to content

Commit

Permalink
Allow helper methods to use blocks in erb views
Browse files Browse the repository at this point in the history
  • Loading branch information
Matias Käkelä authored and rtomayko committed Feb 20, 2009
1 parent 0044683 commit f3ed6e4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/sinatra/base.rb
Expand Up @@ -282,7 +282,7 @@ def template_path(engine, template, options={})

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}"
Expand Down
17 changes: 17 additions & 0 deletions test/erb_test.rb
Expand Up @@ -47,4 +47,21 @@ def erb_app(&block)
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

0 comments on commit f3ed6e4

Please sign in to comment.