Repository: yippee-fun/phlex (Ruby)

Issue:
Rename `content` method
I would like to allow people to use an expose the `content` method as a “slot” for their component. For example, you might want to define a component that has slots for title and content. For example, you could define a `Card` component with `title` and `content` slots:

```ruby
class Card < Phlex::Component
  def template(&)
    article(&)
  end

  def title(text)
    h3 text, class: "title"
  end

  def content(&block)
    div class: "content", &block
  end
end
```

And this component could be used like this:

```ruby
class Example < Phlex::Component
  def template
    render Card do |c|
      c.title "Hi"

      c.content do
        p "Hello!"
      end
    end
  end
end
```

Supporting this means renaming the `content` method defined here:

https://github.com/joeldrapper/phlex/blob/bb358e7942749d7834f679bb8c6e54115d5616cd/lib/phlex/component.rb#L58-L67

This method yields self to the block but also captures the output of that yield as text content if the buffer wasn't changed by the yield. It's difficult to know what to call it.


Discussion:
I think `yield_content(&)` might be a good option, but I'm open to other suggestions. It's actually pretty rare to use this method because you can usually just pass the block to another tag instead.
`capture` perhaps?
`capture` is already defined to return the string value from a block.

https://github.com/joeldrapper/phlex/blob/bb358e7942749d7834f679bb8c6e54115d5616cd/lib/phlex/component.rb#L127-L138

The funny thing is, `capture` would actually work as it is, because it would return a string without modifying the original buffer. Despite that, it's not the right way to do this as it needs to create a new buffer so it's slower.



Patch:
diff --git a/docs/components/tabs.rb b/docs/components/tabs.rb
index 9234e7ad..ef4f27f6 100644
--- a/docs/components/tabs.rb
+++ b/docs/components/tabs.rb
@@ -8,7 +8,7 @@ def initialize
 
 		def template(&block)
 			div class: "tabs flex flex-wrap relative my-5", role: "tablist" do
-				content(&block)
+				yield_content(&block)
 			end
 		end
 
diff --git a/docs/pages/views.rb b/docs/pages/views.rb
index a8e3cf3c..26928ecf 100644
--- a/docs/pages/views.rb
+++ b/docs/pages/views.rb
@@ -18,7 +18,7 @@ class Card < Phlex::View
 							def template(&)
 								article(class: "drop-shadow rounded p-5") {
 									h1 "Amazing content!"
-									content(&)
+									yield_content(&)
 								}
 							end
 						end
diff --git a/lib/phlex/html.rb b/lib/phlex/html.rb
index cbe69a1b..79771b02 100644
--- a/lib/phlex/html.rb
+++ b/lib/phlex/html.rb
@@ -134,7 +134,7 @@ def #{element}(content = nil, **attributes, &block)
               @_target << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[attributes.hash] || _attributes(attributes)) << ">" << CGI.escape_html(content) << "</#{tag}>"
             elsif block_given?
               @_target << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[attributes.hash] || _attributes(attributes)) << ">"
-              content(&block)
+              yield_content(&block)
               @_target << "</#{tag}>"
             else
               @_target << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[attributes.hash] || _attributes(attributes)) << "></#{tag}>"
@@ -151,7 +151,7 @@ def #{element}(content = nil, **attributes, &block)
               end
             elsif block_given?
               @_target << "<#{tag}>"
-              content(&block)
+              yield_content(&block)
               @_target << "</#{tag}>"
             else
               @_target << "<#{tag}></#{tag}>"
diff --git a/lib/phlex/view.rb b/lib/phlex/view.rb
index 5fad90e3..1509e802 100644
--- a/lib/phlex/view.rb
+++ b/lib/phlex/view.rb
@@ -55,7 +55,7 @@ def rendered?
 			register_void_element(method_name, tag: tag)
 		end
 
-		def content(&block)
+		def yield_content(&block)
 			return unless block_given?
 
 			original_length = @_target.length


Test patch:
diff --git a/test/phlex/view/content.rb b/test/phlex/view/content.rb
index e83063be..3d89d213 100644
--- a/test/phlex/view/content.rb
+++ b/test/phlex/view/content.rb
@@ -9,7 +9,7 @@
 		view do
 			def template(&block)
 				div do
-					content(&block)
+					yield_content(&block)
 				end
 			end
 		end