Skip to content

Instantly share code, notes, and snippets.

@joeldrapper
Last active January 25, 2024 06:57
Show Gist options
  • Save joeldrapper/cc82d815357d1b1a5b1d7f53ac8cce82 to your computer and use it in GitHub Desktop.
Save joeldrapper/cc82d815357d1b1a5b1d7f53ac8cce82 to your computer and use it in GitHub Desktop.
Table component
render TableComponent.new(@posts) do |t|
t.column("Title", &:title)
t.column("Author") { |post| post.author.name }
end
class TableComponent < Phlex::HTML
Column = Data.define(:name, :content)
include Phlex::DeferredRender
def initialize(collection)
@collection = collection
@columns = []
end
def template
table do
thead do
tr do
@columns.each do |column|
th { column.name }
end
end
end
tbody do
@collection.each do |item|
tr do
@columns.each do |column|
td do
column.content.call(item)
end
end
end
end
end
end
end
def column(name, &content)
@columns << Column.new(name:, content:)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment