Skip to content

Instantly share code, notes, and snippets.

@bradgessler
Created July 16, 2025 21:36
Show Gist options
  • Select an option

  • Save bradgessler/a919096989c42a9d3fca19b723f36c8e to your computer and use it in GitHub Desktop.

Select an option

Save bradgessler/a919096989c42a9d3fca19b723f36c8e to your computer and use it in GitHub Desktop.
Phlex vs ViewComponent
class BlogPost < Component::Base
def view_template
yield
end
def header(&content)
h1(&content)
end
def post(&content)
div(class: "post", &content)
end
end
## Render in Phlex
render BlogPost.new do |blog|
blog.header { a(href: root_path) { "My Blog" } }
BlogPost.all.each do |post|
blog.post { a(href: post_path(post)) { post.header } }
end
end
class MessageComponent < Component::Base
def initialize(name: "Brad")
@name = name
end
def view_template
h1 { "Hello #{@name.upcase}" }
end
end
render MessageComponent.new(name: "World")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment