Skip to content

Commit

Permalink
Capture should use yield_content
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldrapper committed Feb 10, 2023
1 parent cd94ae1 commit adc124e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.2.0
3.2.1
4 changes: 2 additions & 2 deletions lib/phlex/html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,14 @@ def unsafe_raw(content = nil)
@_target << content
end

def capture
def capture(&block)
return unless block_given?

original_buffer = @_target
new_buffer = +""
@_target = new_buffer

yield
yield_content(&block)

new_buffer
ensure
Expand Down
37 changes: 37 additions & 0 deletions test/phlex/view/capture.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

describe Phlex::HTML do
extend ViewHelper

with "a return value" do
view do
attr_accessor :captured

def template
@captured = capture { "Hello" }
end
end

it "captures the return value" do
example.call
expect(example.captured).to be == "Hello"
end
end

with "a block" do
view do
attr_accessor :captured

def template
@captured = capture do
h1 { "Hello" }
end
end
end

it "captures the return value" do
example.call
expect(example.captured).to be == "<h1>Hello</h1>"
end
end
end

0 comments on commit adc124e

Please sign in to comment.