Module 02 — Phlex Fundamentals
6 lessons · Phlex core · Standalone sandbox
Pure Phlex. No Rails, no Tailwind, no magic. Just Ruby producing HTML.
Every lesson is a single file you run withruby filename.rb.
Before we start
This module is about building understanding, not showing off. The code you write here will sometimes feel more verbose than the ERB equivalent — and occasionally it genuinely is. You’ll write plain "By " where ERB would let you just type “By " inline. You’ll write explicit initialize methods with four lines of assignment that a framework might handle in one. You’ll run scripts in a terminal and read raw HTML output instead of seeing a rendered page.
Bear with it.
The reason we start here — with pure Phlex, no Rails, no Tailwind, no shortcuts — is that every shortcut we introduce later only makes sense if you understand what it’s shortcutting. The Components::Base class in Module 3 will eliminate most of the initialize boilerplate. The component library we build in Modules 3–5 will make the “By <strong>Alice</strong>” problem disappear into a well-named helper. The Tailwind theming in Module 7 will make styling feel like a design system rather than a pile of class strings. Module 9 will show you real-time collaborative UI that would take weeks to build any other way.
None of that lands if you haven’t felt the shape of the thing first.
The goal of this module is simple: by the end of it, you should feel comfortable reading and writing Phlex components, and you should understand why they work the way they do. The reward comes later — and it’s worth it.
Setup
You need one thing: the phlex gem.
|
|
That’s it. No Rails. No database. No asset pipeline. Every lesson file starts with:
|
|
Note: the second line here
require "date"is only needed in some circumstances. At this stage of our understanding, it’s simplest to always require it. (Needless to say, if you’re using Rails you can ignore both of these require statements).
To see the output of any component, we’ll use a tiny helper at the bottom of each file:
|
|
call renders the component and returns an HTML string. puts prints it to the terminal.
Note: the
.callis not something we’d normally use, but is added here just to allow us to see the output from the terminal.
You can pipe it through a formatter if you like:
|
|
“If you want to pretty-print the output for easier reading,
xmllint --format -works on most systems, ortidy -indent -quietif you have it installed. Honestly though, for the short fragments in these lessons, the raw output is readable enough.”
tidy— the classic HTML-specific formatter. More forgiving thanxmllintbecause it actually understands HTML5. Install withbrew install tidy-html5on macOS orsudo apt install tidyon Linux:
Or just read the raw output — it’s valid HTML either way.