Skip to content

Commit

Permalink
Use yield_content in whitespace
Browse files Browse the repository at this point in the history
This will allow the block form of `whitespace` to work the same way you
would work with an element method. Your block can return a string, or
any object that can be formatted by `format_object` if it contains no
other element methods.

```rb
span { "9" }
whitespace { "out of" }
span { "10" }
```
  • Loading branch information
willcosgrove committed Dec 8, 2023
1 parent 3e10cc0 commit 1455b2c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/phlex/sgml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,13 @@ def plain(content)
# Output a whitespace character. This is useful for getting inline elements to wrap. If you pass a block, a whitespace will be output before and after yielding the block.
# @return [nil]
# @yield If a block is given, it yields the block with no arguments.
def whitespace
def whitespace(&block)
target = @_context.target

target << " "

if block_given?
yield
yield_content(&block)
target << " "
end

Expand Down
14 changes: 14 additions & 0 deletions test/phlex/view/whitespace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,18 @@ def template
expect(output).to be == " <a>Home</a> "
end
end

with "whitespace around a string" do
view do
def template
span { "9" }
whitespace { "out of" }
span { "10" }
end
end

it "produces the correct output" do
expect(output).to be == "<span>9</span> out of <span>10</span>"
end
end
end

0 comments on commit 1455b2c

Please sign in to comment.