Created
July 16, 2025 21:36
-
-
Save bradgessler/a919096989c42a9d3fca19b723f36c8e to your computer and use it in GitHub Desktop.
Phlex vs ViewComponent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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