Skip to content

Support multiple views #387

@fsateler

Description

@fsateler

Feature request

Allow refactoring small view parts into a separate view, and be able to render them into the template. These views should not be available outside the component.

app/components/my_component/my_component.html.erb
app/components/my_component/a_part.html.erb
<%# my_component.html.erb %>
<%= render 'a_part' %>

Motivation

Sometimes it is useful to separate a sub-component from the component. For example, if I want to render a popover with extra info about a particular model, I would want to extract that popover into a separate view.

Alternative Solutions:

  1. Extract a new component. Because the popover is not a generic one to be used in other parts of the application, it doesn't make much sense to create a (now public) component for it.
  2. Use content_for. This is not suitable if the partial needs to take arguments. In my example, it wouldn't work if I'm displaying a list of models and I want to show the popover for each model.

Prior art:
ActionView does support partials, of course.
Cells does allow multiple views per cell, and allows using render view: 'a_part'.

Possible solutions I see

  1. Provide a method that prefixes the component dir and then goes on to call render template: with the full path. I would suspect this is relatively slow given that render is slow in ActionView.
  2. Precompile all the templates into methods with a reserved prefix, and allow calling them from the view. For example, using extra_views_{view_name}.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions