Skip to content

Pass args to the block when capturing #716

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/phlex/sgml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,14 @@ def unsafe_raw(content = nil)
# Capture a block of output as a String.
# @note This only works if the block's receiver is the current component or the block returns a String.
# @return [String]
def capture(&block)
def capture(*args, &block)
return "" unless block

@_context.capturing_into(+"") { yield_content(&block) }
if args.length > 0
@_context.capturing_into(+"") { yield_content_with_args(*args, &block) }
else
@_context.capturing_into(+"") { yield_content(&block) }
end
end

private
Expand Down
15 changes: 15 additions & 0 deletions quickdraw/sgml/capture.test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

class ExampleCaptureWithArguments < Phlex::HTML
def view_template(&block)
h1 { capture("a", "b", "c", &block) }
end
end

test "arguments are passed to the capture block" do
output = ExampleCaptureWithArguments.new.call do |a, b, c|
"#{a} #{b} #{c}"
end

expect(output) == "<h1>a b c</h1>"
end