Coming from ERB
If you’ve written ERB, you already know what you want to say. This page shows how to say it in Phlex, one pattern at a time.
The Phlex snippets go inside a component’s view_template method, and instance variables like @article are set in its initialize. Here’s the whole thing once, so the rest of the page can skip it.
class Views::Articles::Show < Views::Base
def initialize(article:)
@article = article
end
def view_template
h1 { @article.title }
end
endElements and attributes
Each element is a method. Attributes are keyword arguments and content goes in a block.
<a href="/about" class="nav-link">About</a>a(href: "/about", class: "nav-link") { "About" }Void elements, such as img, input and br, don’t take a block.
<img src="/logo.png" alt="Logo">img(src: "/logo.png", alt: "Logo")See HTML elements and Attributes.
Outputting values
In ERB, <%= %> outputs a value. In Phlex, when an element’s block returns a string, that string becomes the element’s text. Both escape the value.
<h1><%= @article.title %></h1>h1 { @article.title }Mixing text and elements
The return value is only used when the block hasn’t output anything else. When an element contains text and other elements, use plain for the text.
<p>Hello, <strong><%= @name %></strong>!</p>p do
plain "Hello, "
strong { @name }
plain "!"
endSee Text.
Conditionals
<% if @user.admin? %>
<p>Admin</p>
<% else %>
<p>Guest</p>
<% end %>if @user.admin?
p { "Admin" }
else
p { "Guest" }
endLoops
<ul>
<% @items.each do |item| %>
<li><%= item.name %></li>
<% end %>
</ul>ul do
@items.each do |item|
li { item.name }
end
endConditional classes
Pass an array. nil values are left out.
<a href="/" class="tab <%= "active" if @active %>">Home</a>a(href: "/", class: ["tab", ("active" if @active)]) { "Home" }Data attributes
Pass a hash. Underscores in symbol keys become dashes.
<div data-controller="menu" data-menu-target="panel"></div>div(data: { controller: "menu", menu_target: "panel" })Boolean attributes
true renders the attribute. false and nil leave it out.
<input type="checkbox" <%= "checked" if @done %>>input(type: "checkbox", checked: @done)Whitespace between elements
ERB keeps the whitespace you type. Phlex doesn’t output any unless you ask for it, which matters between inline elements.
<span>One</span> <span>Two</span>span { "One" }
whitespace
span { "Two" }See Whitespace.
Comments
<!-- Navigation -->comment { "Navigation" }Unescaped HTML
In ERB, you’d use raw or html_safe. In Phlex, raw only accepts values you’ve explicitly marked as safe.
<%= raw @trusted_html %>raw safe(@trusted_html)In Rails, a string that’s already html_safe? can be passed to raw directly. See Raw output.
Partials become components
A partial with locals becomes a class with an initializer.
<%# app/views/shared/_avatar.html.erb %>
<img src="<%= user.avatar_url %>" alt="<%= user.name %>" class="avatar"><%= render "shared/avatar", user: @user %>class Components::Avatar < Components::Base
def initialize(user:)
@user = user
end
def view_template
img(src: @user.avatar_url, alt: @user.name, class: "avatar")
end
endrender Components::Avatar.new(user: @user)With a Kit, that’s Avatar(user: @user).
Partials that take a block
A partial that calls yield becomes a component that calls yield.
<%# app/views/shared/_panel.html.erb %>
<section class="panel">
<%= yield %>
</section><%= render "shared/panel" do %>
<p>Hello</p>
<% end %>class Components::Panel < Components::Base
def view_template
section(class: "panel") { yield }
end
endrender Components::Panel do
p { "Hello" }
endWhen you need more than one content area, see Yielding.
Instance variables from the controller
ERB views can read the controller’s instance variables. Phlex views can’t. Pass what the view needs when you create it.
def show
render Views::Articles::Show.new(article: Article.find(params[:id]))
endSee Views.
Rails helpers Rails
Rails’ view helpers aren’t available until you include an adapter for each one. Route helpers are included in Components::Base by the install generator.
<%= link_to "Articles", articles_path %>class Components::Nav < Components::Base
include Phlex::Rails::Helpers::LinkTo
def view_template
link_to "Articles", articles_path
end
endOften you don’t need the helper at all.
a(href: articles_path) { "Articles" }See Helpers for the full list of adapters and Forms for form_with.
Using both at once Rails
You don’t have to convert everything. Phlex components render from ERB:
<%= render Components::Panel.new do %>
<p>Hello from ERB</p>
<% end %>And ERB partials render from Phlex:
render partial("shared/footer")See Rendering and Adopting Phlex.