Repository navigation
feat(sinatra): erb view rendering via go-ruby-erb - #162
Merged
Merged
Conversation
Close the templating half of the Sinatra binding (PR #157 landed routing/dispatch and deferred template rendering). A route/filter action runs against a SinatraCtx (self); the new `erb` helper compiles a template through the already-bound go-ruby-erb compiler and evaluates the resulting Ruby source in the handler's binding — self = the SinatraCtx, so the action's @Ivars and the request helpers (params, request, …) resolve, plus any `locals:` bound as local variables — capturing the _erbout buffer and returning the rendered String, which becomes the Rack response body. Render paths: * inline-String template (needs no filesystem) * :symbol template naming views/<sym>.erb, read via the File binding with the views dir taken from settings :views (default "./views") Trim mode "<>" and eoutvar "_erbout" match Tilt::ERBTemplate's defaults. <%= %> interpolates unescaped, <% %> is control flow. The eval-in-binding uses the front-end (parser + CompileWithLocals), so it is split open/closed: a closed-world binary raises NotImplementedError for `erb`, exactly as Binding#eval / Kernel#eval-on-source already do. Proof (through the rbgo binary, byte-for-byte vs sinatra gem 4.2.1): apps/sinatra_erb_oracle.rb + TestSinatraErbGemOracle exercise inline and :symbol/file templates, param + @ivar interpolation, control flow, positional and options[:locals] locals, and trim behaviour — all rendered identically to the reference gem. 100% coverage on the new binding code. Flagged follow-ups (not binding gaps): * Sinatra 4.x renders :erb via Tilt::ErubiTemplate (erubi); go-ruby-erb mirrors MRI's ERB::Compiler. With trim "<>" the two agree byte-for-byte for single-line and idiomatic multiline templates (a code-only <% %> alone on its line); they diverge only for the unusual line that both opens a code block and ends with an expression tag with trailing content. Full erubi trim-parity is a go-ruby-erb library follow-up. * layouts / partials * :erb escape_html * the Sinatra session store (also deferred by PR #157) Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
tannevaled
added a commit
that referenced
this pull request
Jul 6, 2026
Complete the Sinatra ERB templating begun in #162. The :symbol -> file view path (views/<name>.erb) already landed; this adds the layout rule real Sinatra apps depend on, exactly as MRI Sinatra + Tilt render it: - `erb :view` renders views/layout.erb (if present) with the view's rendered String exposed as `yield`, so the layout's `<%= yield %>` interpolates the inner content; - a missing *default* layout is silently skipped (MRI's catch(:layout_missing)); - `layout: false`/`nil` renders the view with no layout; - `layout: :name` selects a custom layout file (and `layout: true` the default) — a missing *named* layout raises Errno::ENOENT, since MRI eats the error only for the implicit default; - `layout: "src"` uses an inline-String layout. The layout is evaluated in the same handler binding as the view, with the same render locals, and its `yield` returns the view String via a native Proc block threaded through sinatraErbEval. Proven MRI-identical through the rbgo binary: the webapp conformance oracle (scripts/conformance/webapp) gains a second app with a real views/ dir (card.erb + layout.erb + alt.erb) exercising the default layout, layout: false, a custom layout, layout: true, an inline-String layout, and the Errno::ENOENT a missing named layout / view raises; its golden is captured byte-for-byte from the real sinatra gem 4.2.1 + erubi (ruby 4.0.5) and reproduced exactly by rbgo. Internal unit tests cover every new binding branch at 100%. Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes the templating half of the Sinatra binding (PR #157 landed routing/dispatch and explicitly deferred session-store + template rendering).
What it does
A route/filter action runs against a
SinatraCtx(itsself). The newerbhelper::symbolnamingviews/<sym>.erb(read through the File binding; views dir fromsettings :views, default./views);template → Ruby source);self = the SinatraCtx, so the action's@ivarsand the request helpers (params,request, …) resolve, plus anylocals:bound as local variables (positional 3rd arg andoptions[:locals]) — capturing the_erboutbuffer and returning the rendered String, which becomes the Rack response body viasinatraResult.Trim mode
"<>"and eoutvar"_erbout"matchTilt::ERBTemplate's defaults.<%= %>interpolates unescaped,<% %>is control flow.The eval-in-binding uses the front-end (
parser+CompileWithLocals), so it is split open/closed (sinatra_erb_open.go/sinatra_erb_closed.go): a closed-world binary raisesNotImplementedErrorforerb, exactly asBinding#eval/Kernel#eval-on-source already do.Proof — MRI-identical through the rbgo binary
apps/sinatra_erb_oracle.rb+TestSinatraErbGemOracledispatch six routes throughSinatra::Base#call(env)and diff byte-for-byte against the realsinatragem 4.2.1 (MRI 4.0.5): the headlineerb "<h1>Hello <%= params['name'] %> (<%= @n %> visits)</h1>"with an@ivarset in abeforefilter, inline control flow, positional +options[:locals]locals, ERB trim, and a:symbol/file view. All identical.GOWORK=off go build ./... && go vet ./... && go test ./...— greensinatra_erb*.go); merged coverage gate passes at 100.0%prelude_frozen_gen.gountouched (no prelude change)Flagged follow-ups (not binding gaps)
:erbviaTilt::ErubiTemplate(erubi), whereas go-ruby-erb mirrors MRI'sERB::Compiler. With trim"<>"the two agree byte-for-byte for single-line and idiomatic multiline templates (a code-only<% … %>alone on its line). They diverge only for the unusual line that both opens a code block and ends with an expression tag with trailing content (<% each %>x <%= v %>), where MRI ERB trims the trailing newline and erubi keeps it. Full erubi trim-parity is a go-ruby-erb library follow-up.:erbescape_html.🤖 Generated with Claude Code