Skip to content

Commit 9560d6b

Browse files
authored
Pass args to the block when capturing (#716)
When capturing a block, the arguments should be passed along. This is the first step towards fixing the issue with ViewComponent yields in `phlex-rails`.
1 parent 75cd7ca commit 9560d6b

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

lib/phlex/sgml.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,14 @@ def unsafe_raw(content = nil)
193193
# Capture a block of output as a String.
194194
# @note This only works if the block's receiver is the current component or the block returns a String.
195195
# @return [String]
196-
def capture(&block)
196+
def capture(*args, &block)
197197
return "" unless block
198198

199-
@_context.capturing_into(+"") { yield_content(&block) }
199+
if args.length > 0
200+
@_context.capturing_into(+"") { yield_content_with_args(*args, &block) }
201+
else
202+
@_context.capturing_into(+"") { yield_content(&block) }
203+
end
200204
end
201205

202206
private

quickdraw/sgml/capture.test.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
class ExampleCaptureWithArguments < Phlex::HTML
4+
def view_template(&block)
5+
h1 { capture("a", "b", "c", &block) }
6+
end
7+
end
8+
9+
test "arguments are passed to the capture block" do
10+
output = ExampleCaptureWithArguments.new.call do |a, b, c|
11+
"#{a} #{b} #{c}"
12+
end
13+
14+
expect(output) == "<h1>a b c</h1>"
15+
end

0 commit comments

Comments
 (0)