Skip to content

Commit cd2b3be

Browse files
Allow Erubi bufvar to be configured
This allows `render_in` components that compile their own templates to use their view context's current output buffer while a `with_output_buffer` block is being evaluated. Partially addresses rails#39377.
1 parent e4b6c71 commit cd2b3be

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

actionview/lib/action_view/template/handlers/erb/erubi.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ def initialize(input, properties = {})
1414
# Dup properties so that we don't modify argument
1515
properties = Hash[properties]
1616

17+
properties[:bufvar] ||= "@output_buffer"
1718
properties[:preamble] ||= ""
18-
properties[:postamble] ||= "@output_buffer.to_s"
19+
properties[:postamble] ||= "#{properties[:bufvar]}.to_s"
1920

20-
properties[:bufvar] = "@output_buffer"
2121
properties[:escapefunc] = ""
2222

2323
super
@@ -39,7 +39,7 @@ def add_text(text)
3939
if text == "\n"
4040
@newline_pending += 1
4141
else
42-
src << "@output_buffer.safe_append='"
42+
src << bufvar << ".safe_append='"
4343
src << "\n" * @newline_pending if @newline_pending > 0
4444
src << text.gsub(/['\\]/, '\\\\\&')
4545
src << "'.freeze;"
@@ -54,9 +54,9 @@ def add_expression(indicator, code)
5454
flush_newline_if_pending(src)
5555

5656
if (indicator == "==") || @escape
57-
src << "@output_buffer.safe_expr_append="
57+
src << bufvar << ".safe_expr_append="
5858
else
59-
src << "@output_buffer.append="
59+
src << bufvar << ".append="
6060
end
6161

6262
if BLOCK_EXPR.match?(code)
@@ -78,7 +78,7 @@ def add_postamble(_)
7878

7979
def flush_newline_if_pending(src)
8080
if @newline_pending > 0
81-
src << "@output_buffer.safe_append='#{"\n" * @newline_pending}'.freeze;"
81+
src << bufvar << ".safe_append='#{"\n" * @newline_pending}'.freeze;"
8282
@newline_pending = 0
8383
end
8484
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# frozen_string_literal: true
2+
3+
require "abstract_unit"
4+
require "action_view/template/handlers/erb/erubi"
5+
6+
class ErubiTest < ActiveSupport::TestCase
7+
test "can configure bufvar" do
8+
template = <<~ERB
9+
foo
10+
11+
<%= "foo".upcase %>
12+
13+
<%== "foo".length %>
14+
ERB
15+
16+
baseline = ActionView::Template::Handlers::ERB::Erubi.new(template)
17+
erubi = ActionView::Template::Handlers::ERB::Erubi.new(template, bufvar: "boofer")
18+
19+
assert_equal baseline.src.gsub("#{baseline.bufvar}.", "boofer."), erubi.src
20+
end
21+
end

0 commit comments

Comments
 (0)