RenderKit is a small library to that extends rails renderable classes functionality by:
- Allowing renderables to be called by a string identifier, i.e. `render "my_renderable"
- Allowing renderables to be used in place of views with matching controller actions
This project is best used alongside a component library like ViewComponent or Phlex
Add this line to your application's Gemfile:
gem 'render_kit'
And then execute:
$ bundle install
To begin you first need a (Rails renderable class)[rails/rails#36388]
class MyRenderable
def render_in(_)
"Hello World"
end
end
You can register a renderable by calling ActionView::Base.register_renderable
with a string identifier and the renderable class
class MyRenderable
ActionView::Base.register_renderable("my_renderable", self)
def render_in(_)
"Hello World"
end
end
<%= render "my_renderable" %> # => "Hello World"
class MyController < ApplicationController
def index
render MyRenderable.new
end
end
class MyRenderable
ActionView::Base.register_renderable("my_controller_index", self)
def render_in(_)
"Hello World"
end
end
This will render the renderable in place of the view for the index
action in MyController
After checking out the repo, run bin/setup
to install dependencies.The tests use Appraisal to run multiple rails versions(7.0, 7.1, 7.2), i.e for rails 7.2 run bundle exec appraisal rails-7.1 rake test
Bug reports and pull requests are welcome on GitHub. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
Everyone interacting in the RenderKit project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct to ensure fait treatment of everyone, remember MINASWAN.