Phlex for Rails Emails: Action Mailer Without ERB
8 comments
·March 3, 2025axelthegerman
berkes
> Nice write up, however personally I'd take ERB over Phlex a million times.
Agreed.
This has been tried so many times, and non of them really "stuck". HAML, slim, liquid and now phlex. All "DSLs" to some extent that solve the problem of "not having to write HTML".
Is that a problem, really? People writing Rails apps are developing web-apps. Writing HTML is part of the skillset, and of all the things in a Rails app, really not the most difficult, boring or inefficient ones.
I've found the opposite to be true: another layer of abstraction that new hires need to understand. Another DSL to learn. Another syntax/LSP/docs to add to the IDE, or editor. Another leaky abstraction that has to be debugged, monitored, performance-tested. And a very strongly and tightly coupled dependency: it's almost undoable to just move from, say, HAML back to erb in a 10k+LoC rails app: you're effectively married to HAML, and truly in trouble when (not if) the gem gets abandoned.
Now, like Cells[1], Phlex main "problem it solves" isn't "HTML is hard" but "Rails' templates are too simple/poorly designed". Rails templates push a lot of magic (global) state around, is bidirectional (you can update the database from within a template just fine. Both intentional and accidental), it's not isolated, and because of all this, very hard to test. All this becomes worse if you want to re-use components.
But, like Cells, Phlex solves this problem by introducing some DSL to abstract the HTML. Which then makes up the largest part of the gem. Why? Really. OOP templates can be classes that have a "render" and that then use (scoped) "erb" files just fine. This pattern is both easy and available - no gems needed even.
So, yes. My opinionated, advice, fed by decades of struggling with numerous rails projects, is to stay away from such gems: KISS.
pushcx
The thing I really like about Phlex is that it's not a full DSL, it's not its own language. It's an expressive Ruby API and the resulting code has the regular Ruby toolbox available. Ruby devs often call this a "DSL" and I lean away from that use of the term because I think it adds confusion in situations like this. But Phlex feels like a breath of fresh air because it's composable, testable, "just ruby", and has a lot fewer opportunities to footgun myself.
I agree on your criticisms of Erb and ActionView. I'd add that Erb is just really noisy jumping in and out of Ruby with <%= %> constantly. I'm regularly running down basic errors in producing well-formed HTML.
I'm still only experimenting a little with Phlex. I'm not sold on some of its design like the distinction between views and components, but maybe that's a practice/docs issue. I'd also like to see a performance test that's not a microbenchmark and I may try to make one this month. Similar to your note about haml, I've told the project devs that I'd be really encouraged by a phlex -> erb tool to reduce the perceived risk of getting locked into the dep.
EDIT: Oh, and I read the phlex code because I have similar experience outliving a dep. The library is ~1,600 loc with same again in tests. There's a little metaprogramming in SGML::Elements I dislike but it's not a dealbreaker. The library is small and straightforward enough that I'd be surprised if it broke without going unmaintained 5+ years, and I'd be fine maintaining a private fork for a year or so while migrating off.
willcosgrove
>People writing Rails apps are developing web-apps. Writing HTML is part of the skillset, and of all the things in a Rails app, really not the most difficult, boring or inefficient ones.
I don't think of Phlex as something that lets me get away from writing HTML. I see it as a tool that allows me to bring all my experience and skills of writing clean and well organized Ruby code to the view layer. Am I repeating this same bit of markup frequently? Let's extract that out into a method. Are these two pages basically the same, only differing in one section? Perhaps they should share a common base class.
When I first started using Phlex, this wasn't even on my radar. My thought was that it was just a HTML builder DSL in Ruby, so what? The "so what" didn't hit me until working with it for a while. All my views are now Ruby—not text files with bits of interpolated Ruby. And in the 15 years I've been writing Ruby, I've picked up some skills at keeping it maintainable and readable. Skills that I can now apply to the view layer in a way I never could before.
So all of that is to say: I think I understand the reservations you lay out, and I had some of them as well. But I think there is more under the surface if you ever have the opportunity to give it a chance.
And just real quick, regarding this:
> you're [...] truly in trouble when (not if) the gem gets abandoned.
Phlex is sometimes described, somewhat tongue-in-cheek, as a string concatenation toolkit. At its core, it just builds strings. It is unlikely to stop working unless Ruby has some major changes. It is possible that the phlex-rails integration could go stale after a few Rails releases, if it was not being maintained. As long as Rails can send a string response, Phlex will remain usable.
dasil003
Two decades in I still love ruby and rails, but I’m a bit surprised that the subculture promoting aesthetic perfection of code via over-wrought DSLs is still this strong. Haven’t we learned anything about pragmatism since 2009?
dewey
There's a general pattern of:
- People inventing new technologies to not write SQL
- People inventing ways to avoid writing HTML
I feel like the solution to both of these is just to stick to SQL / HTML as for both of these there's a lot of resources, tooling and knowledge and they will not go away. They are transferable between languages, projects and whatever tooling companies are using.3by7
And
- People inventing new technologies to not write JS
- People inventing new technologies to avoid writing CSS
null
Nice write up, however personally I'd take ERB over Phlex a million times.
Sure ERB is another DSL and some things are not perfect. But for the mark-up you write actual HTML instead of having to learn and get used to writing blocks for each element.
I guess to each their own, you enjoy rails with Phlex and I will with ERB.
I think the bigger challenges with emails is that action mailer itself is a little dated and feels cluttered. Most email clients being a nightmare with the actual rendering and not being able to use stylesheets etc makes it even harder... So I'd love to see a general iteration on action mailer to bring it up to the level of Rails 8