Skip to main content
Phlex Hero Light

What is Phlex?

Phlex is a Ruby gem that lets you build object-oriented web views in pure Ruby. Instead of mixing Ruby code with template languages, you write everything in Ruby using a clean, intuitive DSL.
class ArticleComponent < Phlex::HTML
  def initialize(title:)
    @title = title
  end

  def view_template
    article do
      h1(class: "text-2xl font-bold") { @title }
      p { "Welcome to Phlex!" }
    end
  end
end

ArticleComponent.new(title: "Hello World").call
# => <article><h1 class="text-2xl font-bold">Hello World</h1><p>Welcome to Phlex!</p></article>

Why Phlex?

Pure Ruby

No template language to learn. Use Ruby’s full power including classes, modules, and inheritance for your views.

Object-Oriented

Build reusable components with proper encapsulation. Pass data through initializers, not magic variables.

Type-Safe

Leverage Ruby’s type system and tools like Sorbet or RBS. Get autocomplete and type checking in your views.

Lightning Fast

Optimized for performance with attribute caching and efficient buffer management. Faster than ERB or Haml.

Key Features

Component-Based Architecture

Every view is a Ruby class that inherits from Phlex::HTML, Phlex::SVG, or Phlex::CSV. This gives you:
  • Encapsulation: Components manage their own state through instance variables
  • Reusability: Compose components together like building blocks
  • Testability: Unit test your views like any other Ruby class

Clean Attribute Syntax

Phlex provides an elegant API for HTML attributes with automatic safety features:
class ButtonComponent < Phlex::HTML
  def view_template
    button(
      class: "btn btn-primary",
      data: { controller: "button", action: "click->button#toggle" },
      disabled: true
    ) { "Click me" }
  end
end
Phlex automatically converts underscores to dashes in attribute names and provides XSS protection.

Flexible Rendering

Render components in multiple ways:
ArticleComponent.call(title: "Hello")

What You Can Build

HTML Views

Full HTML documents with doctype, meta tags, and semantic markup.

SVG Graphics

Scalable vector graphics with the same clean Ruby syntax.

CSV Exports

Generate CSV files with proper escaping and formatting.

Requirements

Phlex requires Ruby 3.2 or higher. Make sure your Ruby version is up to date.
Phlex has minimal dependencies:
  • zeitwerk (~> 2.7) - For autoloading
  • refract (~> 1.0) - For efficient rendering

Next Steps

Installation

Add Phlex to your project and configure your environment

Quick Start

Build your first component in 5 minutes
Ready to dive deeper? Check out the official Phlex website for advanced patterns and examples.

Build docs developers (and LLMs) love