|
| 1 | +# 1. Allow multiple templates |
| 2 | + |
| 3 | +Date: 2021-08-30 |
| 4 | + |
| 5 | +## Status |
| 6 | + |
| 7 | +Proposed. |
| 8 | + |
| 9 | +## Context |
| 10 | + |
| 11 | +As components become larger (for example, because you are implementing a whole page), it becomes |
| 12 | +useful to be able to extract sections of the view to a different file. ActionView has |
| 13 | +partials, and ViewComponent lacks a similar mechanism. |
| 14 | + |
| 15 | +ActionView partials have the problem that their interface is not introspectable. Data |
| 16 | +may be passed into the partial via ivars or locals, and it is impossible to know |
| 17 | +which without actually opening up the file. Additionally, partials are globally |
| 18 | +invocable, thus making it difficult to detect if a given partial is in use or not, |
| 19 | +and who are its users. |
| 20 | + |
| 21 | +An option would be to extract another Component for the extracted section, but that |
| 22 | +has the following drawbacks: |
| 23 | + |
| 24 | +1. It creates a new public class. |
| 25 | +2. If new component is invoked repeatedly (eg, for a list of items), this creates |
| 26 | + GC pressure by creating lots of intermediate objects. |
| 27 | + |
| 28 | +## Decision |
| 29 | + |
| 30 | +We will allow having multiple templates in the sidecar asset. Each asset will be compiled to |
| 31 | +it's own method `call_<template_name>`. In order to allow the compiled method to receive arguments, |
| 32 | +the component must define them via a `template_arguments :template_name, :argument1, :argument2`. |
| 33 | +This will create required keyword arguments to the `call_<template_name>` method. |
| 34 | + |
| 35 | +## Consequences |
| 36 | + |
| 37 | +This implementation has better performance characteristics over both an extracted component |
| 38 | +and ActionView partials, because it avoids creating intermediate objects, and the overhead of |
| 39 | +creating bindings and `instance_exec`. |
| 40 | +Having explicit arguments makes the interface explicit. |
| 41 | + |
| 42 | +TODO: The following are consequences of the current approach, but the approach might be extended |
| 43 | +to avoid them: |
| 44 | + |
| 45 | +The interface to render a sidecar partial would be a method call, and depart from the usual |
| 46 | +`render(*)` interface used in ActionView. |
| 47 | + |
| 48 | +The generated methods are only invokable via keyword arguments |
| 49 | + |
| 50 | +The generated methods cannot have arguments with default values. |
| 51 | + |
| 52 | +The generated methods are public, and thus could be invoked by a third party. |
0 commit comments