Skip to content

feat(sinatra): erb view rendering via go-ruby-erb - #162

Merged
tannevaled merged 1 commit into
mainfrom
feat/sinatra-erb-views
Jul 6, 2026
Merged

tannevaled merged 1 commit into
mainfrom
feat/sinatra-erb-views

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

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 (its self). The new erb helper:

  1. resolves the template — an inline String or a :symbol naming views/<sym>.erb (read through the File binding; views dir from settings :views, default ./views);
  2. compiles it through the already-bound go-ruby-erb compiler (template → Ruby source);
  3. evaluates that 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 (positional 3rd arg and options[:locals]) — capturing the _erbout buffer and returning the rendered String, which becomes the Rack response body via sinatraResult.

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 (sinatra_erb_open.go / sinatra_erb_closed.go): a closed-world binary raises NotImplementedError for erb, exactly as Binding#eval / Kernel#eval-on-source already do.

Proof — MRI-identical through the rbgo binary

apps/sinatra_erb_oracle.rb + TestSinatraErbGemOracle dispatch six routes through Sinatra::Base#call(env) and diff byte-for-byte against the real sinatra gem 4.2.1 (MRI 4.0.5): the headline erb "<h1>Hello <%= params['name'] %> (<%= @n %> visits)</h1>" with an @ivar set in a before filter, inline control flow, positional + options[:locals] locals, ERB trim, and a :symbol/file view. All identical.

  • GOWORK=off go build ./... && go vet ./... && go test ./... — green
  • rbgo real-Ruby conformance — green
  • 100% coverage on the new binding code (sinatra_erb*.go); merged coverage gate passes at 100.0%
  • prelude_frozen_gen.go untouched (no prelude change)

Flagged follow-ups (not binding gaps)

  • Sinatra 4.x renders :erb via Tilt::ErubiTemplate (erubi), whereas 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 (<% 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.
  • Layouts / partials (stretch).
  • :erb escape_html.
  • The Sinatra session store (also deferred by PR fix(sinatra): persist handler @ivars across before/route/after + MRI-identity oracle vs sinatra gem 4.2.1 #157).

🤖 Generated with Claude Code

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
tannevaled merged commit 6186912 into main Jul 6, 2026
9 checks passed
@tannevaled
tannevaled deleted the feat/sinatra-erb-views branch July 6, 2026 14:11
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>
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.

1 participant