From f3ed6e4dd99522767b27b637dfb100a4628613c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matias=20K=C3=A4kel=C3=A4?= Date: Wed, 18 Feb 2009 20:26:06 +0200 Subject: [PATCH] Allow helper methods to use blocks in erb views --- lib/sinatra/base.rb | 2 +- test/erb_test.rb | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index 1c3881e26b..37fab96290 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -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}" diff --git a/test/erb_test.rb b/test/erb_test.rb index e3fd9dcfdd..df3902d2bd 100644 --- a/test/erb_test.rb +++ b/test/erb_test.rb @@ -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