The project is in a healthy, maintained state
Write Phlex components directly in .html.rb view files
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Runtime

>= 1.0
>= 6.0
 Project Readme

Phlex::Rails::Template

A Rails template handler that lets you write Phlex components directly in .html.rb view files.

Installation

Run the following command from the root of your Rails project:

# Make sure you've installed phlex-rails
bundle add 'phlex-rails-template'

Usage

Create view files with the .html.rb extension:

# app/views/posts/show.html.rb
h1 { @post.title }

div(class: "content") do
  p { @post.body }
end

In your controller:

class PostsController < ApplicationController
  def show
    @post = Post.find(params[:id])
    # Renders app/views/posts/show.html.rb automatically
  end
end

Controller instance variables are automatically available in your templates.

Configuration

You can customize how components are instantiated and how variables are assigned by passing a block to register:

# config/initializers/phlex_rails_template.rb
Phlex::Rails::Template.register :rb do
  # Override the base component class
  def component_class
    ApplicationComponent
  end

  # Override to instantiate the component with custom arguments
  def create_component(component_class)
    component_class.new(view_context.session, request: view_context.request)
  end

  # Override to customize how controller variables are assigned
  def assign_variables
    view_context.assigns.each do |key, value|
      component.instance_variable_set(:"@#{key}", "PREFIX: #{value}")
    end
  end
end

Or you can pass a configurator class directly:

class CustomConfigurator < Phlex::Rails::Template::Configurator
  def component_class
    ApplicationComponent
  end
end

Phlex::Rails::Template.register :rb, CustomConfigurator

You can also register additional template handlers with different configurators:

Phlex::Rails::Template.register :customrb, MyCustomConfigurator

License

The gem is available as open source under the terms of the MIT License.