Adds support for sub-templates with typed arguments to ViewComponent, enabling modular, reusable component architectures.
✅ Template Arguments - Define typed arguments for sub-templates
✅ Automatic Detection - Sub-templates discovered in component sidecar directories
✅ Dynamic Methods - call_[name] helper methods generated automatically
✅ ViewComponent Integration - Seamless integration with existing ViewComponent workflow
Add this line to your application's Gemfile:
gem 'view_component_subtemplates'And then execute:
bundle install# app/components/table_component.rb
class TableComponent < ViewComponent::Base
def initialize(users:, title:)
@users = users
@title = title
end
end<!-- app/components/table_component.html.erb -->
<div class="table-container">
<%= call_header(title: @title, sortable: true) %>
<tbody>
<% @users.each_with_index do |user, index| %>
<%= call_row(model: user, highlight: index.even?) %>
<% end %>
</tbody>
<%= call_footer(total_count: @users.count) %>
</div>app/components/
├── table_component.rb
├── table_component.html.erb
└── table_component/
├── header.html.erb
├── row.html.erb
└── footer.html.erb
<!-- app/components/table_component/header.html.erb -->
<%# locals: (title:, sortable:) -%>
<thead class="<%= 'sortable' if sortable %>">
<tr>
<th><%= title %></th>
<th>Actions</th>
</tr>
</thead><!-- app/components/table_component/row.html.erb -->
<%# locals: (model:, highlight:) -%>
<tr class="<%= 'highlighted' if highlight %>">
<td><%= model.name %></td>
<td><%= model.email %></td>
</tr><!-- app/components/table_component/footer.html.erb -->
<%# locals: (total_count:) -%>
<tfoot>
<tr>
<td colspan="2">Total: <%= total_count %> users</td>
</tr>
</tfoot><%= render TableComponent.new(users: @users, title: "User List") %>- Ruby >= 3.1.0
- Rails >= 7.0.0
- ViewComponent >= 4.2.0
After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests.
bundle install
bundle exec rake test