Ruby's ERB template compiler in pure Go — MRI-compatible byte-for-byte, no cgo.
🌐 Website · 📚 Documentation
go-ruby-erb is a pure-Go (no cgo) reimplementation of ERB,
Ruby's templating system — the deterministic, interpreter-independent core of
MRI's ERB::Compiler. It turns an ERB template string into the Ruby source
that renders it, matching MRI 4.0.5 byte-for-byte, and provides ERB::Util's
html_escape / url_encode helpers.
Compiling a template — tag scanning, trim modes, the <%% %%> literals,
magic-comment detection, the String#dump text encoding — is fully
deterministic and needs no interpreter, so it lives here as pure Go. The
final eval(compiled_src, binding) that produces the rendered string does
need a Ruby interpreter and stays in the consumer (e.g. rbgo).
This library compiles; the host evaluates.
It is standalone and reusable by any Go program, and is the ERB backend for the sibling org go-embedded-ruby.
| Repo | What it is |
|---|---|
| erb | the compiler: tag scanner, trim modes, the Compile / Compiler API, and ERB::Util (HTMLEscape / URLEncode) |
| docs | MkDocs Material documentation, versioned with mike, served at /docs/ |
| go-ruby-erb.github.io | the Hugo landing page |
| brand | logos and brand assets |
- Pure Go, zero cgo. Cross-compiles and embeds anywhere; a static binary by default.
- A compiler, not a renderer. It emits MRI-byte-exact Ruby source plus the
magic comment; the rendering
evalneeds an interpreter and lives downstream — that split is what lets the deterministic core ship as pure Go. - MRI byte-exact. Emitted source and rendered output match the reference
ERB::Compilerexactly, validated by a differential oracle against therubybinary. - Standalone & reusable. No dependency on the Ruby runtime; the dependency runs the other way.
- 100% test coverage is the target, enforced as a CI gate.
Compiler complete — MRI byte-exact. A faithful port of MRI's
lib/erb/compiler.rb: all tag kinds (<% %>, <%= %>, <%# %>), the
<%% %%> literals, every trim mode (-, >, <>, % and combinations),
magic-comment detection (incl. the emacs -*- … -*- form), and binary-exact
literal-run encoding via Ruby's String#dump, plus ERB::Util's HTMLEscape /
URLEncode. Differential-tested against MRI 4.0.5 — emitted source and rendered
output compared byte-for-byte — at 100% coverage, gofmt + go vet clean, CI
green across the six 64-bit Go targets (amd64, arm64, riscv64, loong64, ppc64le,
s390x). The rendering eval is downstream in the go-embedded-ruby consumer, not
part of this module.
BSD-3-Clause.
