A Tale of two Phlexes
There are two types of components you can write in Phlex. The standard component exposes a “builder” style API. The second type is a component that uses DeferredRender
, which fully consumes the render block before its own template is rendered.
Let’s take a look at both types by building two different Table components.
The first component will use a builder style API.
class Table < Phlex::HTML
def template
table do
yield
end
end
def head
thead do
tr do
yield
end
end
end
def column_heading
th { yield }
end
def body
tbody { yield }
end
def row
tr { yield }
end
def cell
td { yield }
end
end
And we can imagine how we might use it:
render Table.new do |t|
t.head do
t.column_heading { "Name" }
t.column_heading { "Age" }
end
t.body do
t.row do
t.cell { "Alice" }
t.cell {
...