Skip to content

Add external_template_mode configuration option - #115

Merged
marcoroth merged 1 commit into
mainfrom
add-external-template-mode
Aug 8, 2026
Merged

Add external_template_mode configuration option#115
marcoroth merged 1 commit into
mainfrom
add-external-template-mode

Conversation

@marcoroth

Copy link
Copy Markdown
Owner

Follow up on #94.

This pull request adds config.external_template_mode to control what happens to templates that come from gems rather than from the application itself.

Motivation

With intercept_erb enabled, ReActionView sees every .html.erb template Rails renders, including ones shipped inside gems. #94 stopped compiling those, which fixed #91, but it did so silently. If a gem's templates cannot be compiled by Herb, you never find out.

That silence is the objection @joelhawksley raises in marcoroth/herb#1508, where a vendored copy of primer/view_components contains ERB that Herb cannot compile:

So rather than a binary "skip or don't", the mode says how loudly to handle the failure:

Mode Behavior
:fallback (default) Compile with Herb. If that fails, log a warning and fall back to ActionView::Template::Handlers::ERB, so the template renders exactly as it would without ReActionView installed.
:skip Never compile templates that come from gems. This is what #94 shipped.
:compile No special treatment. Your validation_mode applies to them just as it does to your own templates, and nothing is rescued.
ReActionView.configure do |config|
  config.external_template_mode = :fallback
end

A gem template Herb cannot handle now renders as before, and says so:

[ReActionView] /app/vendor/bundle/ruby/3.4.0/gems/actionpack-8.1.2/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb
could not be compiled by Herb, falling back to ActionView::Template::Handlers::ERB:
InvalidNestingError: Block element <h2> cannot be nested inside <p> at line 9

Related marcoroth/herb#1508
Related marcoroth/herb#1362
Related #91

@marcoroth
marcoroth merged commit 42fe047 into main Aug 8, 2026
24 checks passed
@marcoroth
marcoroth deleted the add-external-template-mode branch August 8, 2026 03:15
marcoroth added a commit that referenced this pull request Aug 8, 2026
Follow up on #115.

When a gem ships a `.html.herb` template, it reaches
`ReActionView::Template::Handlers::Herb` straight from the railtie. It
never passes through `Handlers::ERB`, so nothing is in a position to
rescue it.

#115 nonetheless forced `validation_mode: :raise` for every external
template, including those, which turned what used to be a validation
overlay into an uncaught exception on a template the application cannot
edit:

```ruby
ReActionView.config.validation_mode = :overlay
ReActionView.config.external_template_mode = :fallback

# /gems/some_gem/app/views/x.html.herb
# before: Herb::Engine::CompilationError
# after:  compiles, with the usual validation overlay
```

The mistake was putting the decision in `Handlers::Herb`, which cannot
tell whether its caller will rescue. `validation_mode_for` is gone, and
the mode is now passed in by the handler that owns the `rescue`.

Related #115
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ReactionView is Cranky About Internal Rails Templates

1 participant