view_component_subtemplates 0.2.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +17 -0
- data/README.md +42 -1
- data/lib/view_component_subtemplates/standalone_renderer.rb +44 -0
- data/lib/view_component_subtemplates/sub_template.rb +47 -6
- data/lib/view_component_subtemplates/version.rb +1 -1
- data/lib/view_component_subtemplates.rb +2 -0
- metadata +6 -8
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 6db77273514e3dbdc8bbcd2b15a3a446ce1bf77887dbfec439bafccdc1958ef6
|
|
4
|
+
data.tar.gz: fd18610333ffa99b8c9fc937a786f53280990b3e659d160cf3d7cf5849e87676
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 50e2ba1c63fbdc320ed5a673972410992ae3f8561280bce9075cbeb6d5b798eb8bc20b9586b2e40edd5d0a7a0ecac8f791427b931f15d4014d01d99387dd1a4d
|
|
7
|
+
data.tar.gz: 82fb32bf3c9115346aa2b4839655bf484229aac232f017ced62f603dbc634ebe835b0ab7ddfb85ab07941244d40871a9dc9a019d4e13467f9ade0056ca30bdb9
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
## [Unreleased]
|
|
2
2
|
|
|
3
|
+
## [0.4.0] - 2026-11-20
|
|
4
|
+
|
|
5
|
+
### Fixed
|
|
6
|
+
- Subtemplates now escape interpolated values. They were compiled with plain `ERB` and the whole output was marked `html_safe`, so user input rendered through `<%= %>` was inserted as raw HTML. Values already marked `html_safe` are still rendered as-is.
|
|
7
|
+
|
|
8
|
+
### Changed
|
|
9
|
+
- **Breaking:** requires `view_component >= 4.10.0`. Subtemplates render through the component's `capture`, which ViewComponent syncs with the view context's buffer since 4.10.0. With earlier versions, block helpers called through `helpers` render their content outside the container.
|
|
10
|
+
- **Breaking:** block helpers that return their output (`fields_for`, `simple_fields_for`, `form_with`, `content_tag`, etc.) must be called with `<%=`, as in any Rails template. With plain `ERB`, `<% fields_for :x do |f| %>` rendered its block by accident; it now renders nothing, without raising an error. Change those tags to `<%= fields_for :x do |f| %>` when upgrading. This change can't be made before upgrading: plain `ERB` raises a syntax error on `<%= helper do %>`.
|
|
11
|
+
- Subtemplates are compiled with the ActionView handler registered for their extension, the same way ViewComponent compiles the main template. Block helpers (`<%= content_tag :div do %>`) and `capture` now work inside subtemplates, and `.haml`/`.slim` subtemplates use their own handler instead of being compiled as ERB.
|
|
12
|
+
- The trim marker in `<%# locals: (...) -%>` now removes the line break that follows it, so subtemplate output no longer starts with `"\n"`.
|
|
13
|
+
- Subtemplates in a non-HTML format (e.g. `notes.text.erb`) are not HTML-escaped, following Rails' behavior for text templates. Their output is not marked `html_safe`, so it is escaped when embedded in an HTML template.
|
|
14
|
+
|
|
15
|
+
## [0.3.0] - 2026-06-12
|
|
16
|
+
|
|
17
|
+
### Added
|
|
18
|
+
- `render_subtemplate_in(view_context, name)` to render a single subtemplate on its own, from outside the component (e.g. a controller responding with just a fragment), without rendering the main template. It is the subtemplate-level counterpart of ViewComponent's `render_in`. The subtemplate must not declare locals (per-render data is passed through the component's constructor); rendering a subtemplate with locals, or one that is not defined, raises a clear error.
|
|
19
|
+
|
|
3
20
|
## [0.2.0] - 2026-01-21
|
|
4
21
|
|
|
5
22
|
### Added
|
data/README.md
CHANGED
|
@@ -107,11 +107,52 @@ app/components/
|
|
|
107
107
|
<%= render TableComponent.new(users: @users, title: "User List") %>
|
|
108
108
|
```
|
|
109
109
|
|
|
110
|
+
## Rendering a subtemplate on its own
|
|
111
|
+
|
|
112
|
+
Sometimes you need to render just one subtemplate, outside the component's main
|
|
113
|
+
template. A common case is a controller action that responds to an AJAX request
|
|
114
|
+
with only an updated fragment.
|
|
115
|
+
|
|
116
|
+
Call `render_subtemplate_in` on a component instance, passing the current view
|
|
117
|
+
context. The subtemplate **must not declare locals**: any per-render data is
|
|
118
|
+
passed through the component's constructor, so the call site stays free of an
|
|
119
|
+
untyped `**locals` boundary.
|
|
120
|
+
|
|
121
|
+
```ruby
|
|
122
|
+
class UsersController < ApplicationController
|
|
123
|
+
def row
|
|
124
|
+
component = RowComponent.new(user: User.find(params[:id]), highlight: false)
|
|
125
|
+
|
|
126
|
+
render html: component.render_subtemplate_in(view_context, :row), layout: false
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
```erb
|
|
132
|
+
<%# app/components/row_component/row.html.erb -- no `locals:` line %>
|
|
133
|
+
<tr class="<%= 'highlighted' if @highlight %>"><td><%= @user.name %></td></tr>
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
`render_subtemplate_in(view_context, name)` renders the named subtemplate and
|
|
137
|
+
returns its HTML as an html_safe string, without rendering the component's main
|
|
138
|
+
template. It is the subtemplate-level counterpart of ViewComponent's `render_in`:
|
|
139
|
+
`render_in(view_context)` is the external entry point for the main template
|
|
140
|
+
(internally `call`), and `render_subtemplate_in` is the external entry point for
|
|
141
|
+
a single subtemplate (internally `call_<name>`).
|
|
142
|
+
|
|
143
|
+
`render_subtemplate_in` raises a clear error if the named subtemplate does not
|
|
144
|
+
exist, or if it declares locals (pass that data through the component's
|
|
145
|
+
constructor instead).
|
|
146
|
+
|
|
147
|
+
**The no-locals rule applies only to standalone rendering.** Inside a component's
|
|
148
|
+
templates, `call_<name>` keeps taking locals exactly as shown in the Quick Start;
|
|
149
|
+
`render_subtemplate_in` is the only path that requires a no-locals subtemplate.
|
|
150
|
+
|
|
110
151
|
## Requirements
|
|
111
152
|
|
|
112
153
|
- Ruby >= 3.1.0
|
|
113
154
|
- Rails >= 7.0.0
|
|
114
|
-
- ViewComponent >= 4.
|
|
155
|
+
- ViewComponent >= 4.10.0
|
|
115
156
|
|
|
116
157
|
## Development
|
|
117
158
|
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ViewComponentSubtemplates
|
|
4
|
+
# Renders one subtemplate on its own (no main template), e.g. to serve a fragment
|
|
5
|
+
# from a controller. The subtemplate-level counterpart of `render_in`.
|
|
6
|
+
# Requires a no-locals subtemplate: per-render data goes through the constructor.
|
|
7
|
+
module StandaloneRenderer
|
|
8
|
+
# @param view_context [ActionView::Base] inside a controller, this is `view_context`.
|
|
9
|
+
# @param name [Symbol, String] subtemplate name, without the `call_` prefix.
|
|
10
|
+
# @return [String] the subtemplate's HTML (html_safe).
|
|
11
|
+
def render_subtemplate_in(view_context, name)
|
|
12
|
+
self.class.__vc_compile(raise_errors: true)
|
|
13
|
+
ensure_renderable_subtemplate!(name)
|
|
14
|
+
|
|
15
|
+
# Set up the view context the helper guards require. `__vc_original_view_context`
|
|
16
|
+
# makes `helpers` reuse it instead of building a new one (keep both).
|
|
17
|
+
@view_context = view_context
|
|
18
|
+
self.__vc_original_view_context = view_context
|
|
19
|
+
|
|
20
|
+
public_send("call_#{name}")
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
def ensure_renderable_subtemplate!(name)
|
|
26
|
+
call_method = "call_#{name}"
|
|
27
|
+
unless respond_to?(call_method)
|
|
28
|
+
raise ViewComponentSubtemplates::Error,
|
|
29
|
+
"Subtemplate `#{name}` is not defined on #{self.class}. " \
|
|
30
|
+
"Available subtemplates: #{available_subtemplates.join(", ").presence || "(none)"}."
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
return if method(call_method).parameters.empty?
|
|
34
|
+
|
|
35
|
+
raise ViewComponentSubtemplates::Error,
|
|
36
|
+
"Subtemplate `#{name}` declares locals; standalone rendering requires a subtemplate " \
|
|
37
|
+
"without locals. Pass per-render data through the component's constructor instead."
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def available_subtemplates
|
|
41
|
+
methods.grep(/\Acall_(.+)\z/) { Regexp.last_match(1) }
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -15,6 +15,10 @@ module ViewComponentSubtemplates
|
|
|
15
15
|
#
|
|
16
16
|
# PRIVATE API - All other methods are subject to change without notice.
|
|
17
17
|
class SubTemplate
|
|
18
|
+
# Minimal template object accepted by ActionView template handlers.
|
|
19
|
+
TemplateDetails = Struct.new(:format, :identifier, :short_identifier, :type, keyword_init: true)
|
|
20
|
+
private_constant :TemplateDetails
|
|
21
|
+
|
|
18
22
|
# PUBLIC API
|
|
19
23
|
attr_reader :component, :path, :template_name
|
|
20
24
|
|
|
@@ -37,7 +41,7 @@ module ViewComponentSubtemplates
|
|
|
37
41
|
# This is the main entry point for the class from the compiler extension
|
|
38
42
|
def compile_to_component
|
|
39
43
|
validate_file_exists!
|
|
40
|
-
compiled_source =
|
|
44
|
+
compiled_source = compile_source
|
|
41
45
|
define_render_method(compiled_source, explicit_locals)
|
|
42
46
|
end
|
|
43
47
|
|
|
@@ -57,10 +61,40 @@ module ViewComponentSubtemplates
|
|
|
57
61
|
locals_string.scan(/(\w+):/).flatten.map(&:to_sym)
|
|
58
62
|
end
|
|
59
63
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
+
# Compiles the source with the ActionView handler registered for the file
|
|
65
|
+
# extension, the same way ViewComponent compiles the main template. This keeps
|
|
66
|
+
# Rails' HTML escaping for `<%= %>` and support for block helpers and capture.
|
|
67
|
+
def compile_source
|
|
68
|
+
template = TemplateDetails.new(
|
|
69
|
+
format: template_format,
|
|
70
|
+
identifier: path,
|
|
71
|
+
short_identifier: short_identifier,
|
|
72
|
+
type: template_type
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
template_handler.call(template, source)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def template_type
|
|
79
|
+
ActionView::Template::Types[template_format]
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def escaped_output?
|
|
83
|
+
!ActionView::Template::Handlers::ERB.escape_ignore_list.include?(template_type)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def template_handler
|
|
87
|
+
extension = File.basename(path).split(".").last
|
|
88
|
+
ActionView::Template.registered_template_handler(extension)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def template_format
|
|
92
|
+
parts = File.basename(path).split(".")
|
|
93
|
+
parts.length > 2 ? parts[-2].to_sym : :html
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def short_identifier
|
|
97
|
+
defined?(Rails.root) && Rails.root ? path.sub("#{Rails.root}/", "") : path
|
|
64
98
|
end
|
|
65
99
|
|
|
66
100
|
def define_render_method(compiled_source, expected_args)
|
|
@@ -71,11 +105,18 @@ module ViewComponentSubtemplates
|
|
|
71
105
|
define_public_call_method(call_method_name, render_method_name, expected_args)
|
|
72
106
|
end
|
|
73
107
|
|
|
108
|
+
# `capture` renders into a fresh string inside the shared buffer, not the
|
|
109
|
+
# caller's output. ViewComponent's override syncs it with the view context's
|
|
110
|
+
# buffer (they differ inside partials), so `helpers` blocks capture correctly.
|
|
111
|
+
# It returns nil for an empty subtemplate.
|
|
74
112
|
def define_private_render_method(name, compiled_source, args)
|
|
113
|
+
output = escaped_output? ? "output" : "String.new(output)"
|
|
114
|
+
|
|
75
115
|
@component.class_eval <<~RUBY, __FILE__, __LINE__ + 1
|
|
76
116
|
private
|
|
77
117
|
def #{name}(#{args.join(", ")})
|
|
78
|
-
|
|
118
|
+
output = capture { #{compiled_source} } || "".html_safe
|
|
119
|
+
#{output}
|
|
79
120
|
end
|
|
80
121
|
RUBY
|
|
81
122
|
end
|
|
@@ -37,8 +37,10 @@ end
|
|
|
37
37
|
|
|
38
38
|
# Load SubTemplate after the module is fully configured
|
|
39
39
|
require_relative "view_component_subtemplates/sub_template"
|
|
40
|
+
require_relative "view_component_subtemplates/standalone_renderer"
|
|
40
41
|
|
|
41
42
|
# Hook into ViewComponent when it loads (lazy loading for faster boot in development)
|
|
42
43
|
ActiveSupport.on_load(:view_component) do
|
|
43
44
|
ViewComponent::Base.singleton_class.prepend(ViewComponentSubtemplates::AfterCompileHook)
|
|
45
|
+
ViewComponent::Base.include(ViewComponentSubtemplates::StandaloneRenderer)
|
|
44
46
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: view_component_subtemplates
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- jsolas
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: exe
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
13
|
name: rails
|
|
@@ -30,14 +29,14 @@ dependencies:
|
|
|
30
29
|
requirements:
|
|
31
30
|
- - ">="
|
|
32
31
|
- !ruby/object:Gem::Version
|
|
33
|
-
version: 4.
|
|
32
|
+
version: 4.10.0
|
|
34
33
|
type: :runtime
|
|
35
34
|
prerelease: false
|
|
36
35
|
version_requirements: !ruby/object:Gem::Requirement
|
|
37
36
|
requirements:
|
|
38
37
|
- - ">="
|
|
39
38
|
- !ruby/object:Gem::Version
|
|
40
|
-
version: 4.
|
|
39
|
+
version: 4.10.0
|
|
41
40
|
description: Adds support for sub-templates with arguments to ViewComponent, allowing
|
|
42
41
|
components to have multiple templates in their sidecar directory.
|
|
43
42
|
email:
|
|
@@ -53,6 +52,7 @@ files:
|
|
|
53
52
|
- Rakefile
|
|
54
53
|
- lib/view_component_subtemplates.rb
|
|
55
54
|
- lib/view_component_subtemplates/compiler_extension.rb
|
|
55
|
+
- lib/view_component_subtemplates/standalone_renderer.rb
|
|
56
56
|
- lib/view_component_subtemplates/sub_template.rb
|
|
57
57
|
- lib/view_component_subtemplates/version.rb
|
|
58
58
|
homepage: https://github.com/bukhr/view_component_subtemplates
|
|
@@ -63,7 +63,6 @@ metadata:
|
|
|
63
63
|
homepage_uri: https://github.com/bukhr/view_component_subtemplates
|
|
64
64
|
source_code_uri: https://github.com/bukhr/view_component_subtemplates
|
|
65
65
|
changelog_uri: https://github.com/bukhr/view_component_subtemplates/blob/main/CHANGELOG.md
|
|
66
|
-
post_install_message:
|
|
67
66
|
rdoc_options: []
|
|
68
67
|
require_paths:
|
|
69
68
|
- lib
|
|
@@ -78,8 +77,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
78
77
|
- !ruby/object:Gem::Version
|
|
79
78
|
version: '0'
|
|
80
79
|
requirements: []
|
|
81
|
-
rubygems_version:
|
|
82
|
-
signing_key:
|
|
80
|
+
rubygems_version: 4.0.15
|
|
83
81
|
specification_version: 4
|
|
84
82
|
summary: Sub-templates functionality for ViewComponent
|
|
85
83
|
test_files: []
|