Skip to content

Commit

Permalink
Flip result and code for :return_buffer option
Browse files Browse the repository at this point in the history
  • Loading branch information
evanleck committed Oct 23, 2018
1 parent d822461 commit c3eac9f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/erubi/capture_end.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ def handle(indicator, code, tailch, rspace, lspace)
when '|'
rspace = nil if tailch && !tailch.empty?
add_text(lspace) if lspace
result = @return_buffer ? "; #{@bufvar}; " : ""
src << code << result << ")).to_s; ensure; #{@bufvar} = #{@bufstack}.pop; end;"
result = @return_buffer ? " #{@bufvar}; " : ""
src << result << code << ")).to_s; ensure; #{@bufvar} = #{@bufstack}.pop; end;"
add_text(rspace) if rspace
else
super
Expand Down
55 changes: 55 additions & 0 deletions test/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,61 @@ def self.bar
LET'S EAT SALADS!
B
END3
end

it "should respect the :return_buffer option for making templates return the (potentially modified) buffer as the result of the block" do
@options[:engine] = ::Erubi::CaptureEndEngine
@options[:return_buffer] = true

def self.bar(foo = nil)
if foo.nil?
yield
else
foo
end
end

check_output(<<END1, <<END2, <<END3) {}
<%|= bar do %>
Let's eat the tacos!
<%| end %>
Delicious!
END1
_buf = String.new;begin; (__erubi_stack ||= []) << _buf; _buf = String.new; __erubi_stack.last << (( bar do _buf << '
'; _buf << 'Let\\'s eat the tacos!
'; _buf; end )).to_s; ensure; _buf = __erubi_stack.pop; end; _buf << '
'; _buf << '
Delicious!
';
_buf.to_s
END2
Let's eat the tacos!
Delicious!
END3

check_output(<<END1, <<END2, <<END3) {}
<%|= bar("Don't eat the burgers!") do %>
Let's eat burgers!
<%| end %>
Delicious!
END1
_buf = String.new;begin; (__erubi_stack ||= []) << _buf; _buf = String.new; __erubi_stack.last << (( bar(\"Don't eat the burgers!\") do _buf << '
'; _buf << 'Let\\'s eat burgers!
'; _buf; end )).to_s; ensure; _buf = __erubi_stack.pop; end; _buf << '
'; _buf << '
Delicious!
';
_buf.to_s
END2
Don't eat the burgers!
Delicious!
END3
end
end

0 comments on commit c3eac9f

Please sign in to comment.