Class: BarefootJS::Backend::Erb
- Inherits:
-
Object
- Object
- BarefootJS::Backend::Erb
- Defined in:
- lib/barefoot_js/backend/erb.rb
Overview
ERB rendering backend for the BarefootJS runtime.
The engine-agnostic runtime logic -- the JS-compat value helpers,
array/string methods, hydration markers, child rendering -- lives in
BarefootJS::Context. This backend supplies the four engine-specific
operations the runtime delegates to, targeting Ruby stdlib ERB:
encode_json(data) -> JSON string (injectable encoder)
mark_raw(str) -> identity (ERB has no "safe string"
wrapper type -- ERB's own `<%=` is
NOT auto-escaping, so the compiled
templates that need escaping call
`bf.h(...)` explicitly; `mark_raw`
only exists so runtime helpers that
already produce finished HTML --
e.g. spread_attrs -- share one
interface with the Kolon/EP ports)
materialize(value) -> resolve a captured-children value
to a string
render_named(name, bf, vars) -> render `<name>.erb` with `bf` and
`v` (vars, symbol-keyed) bound
Pair it with the @barefootjs/erb compile-time adapter, which emits
.erb templates that call the runtime as a bf local: <%= bf.h(v[:x]) %>, <%= bf.spread_attrs(bag) %>. Unlike the Perl backends, this has
no dependency on a web framework: a plain directory of .erb files
renders under any Rack app (or none at all).
Defined Under Namespace
Classes: Renderer
Instance Method Summary collapse
- #encode_json(data) ⇒ Object
-
#initialize(path:, json_encoder: nil, cache: true) ⇒ Erb
constructor
ERB template locals contract (spec/compiler.md "ERB emission contract"): every compiled template receives exactly two locals --
bf(the BarefootJS::Context for this render) andv(a Hash with SYMBOL keys holding every prop/signal/memo/module-constant the template references). -
#mark_raw(str) ⇒ Object
ERB has no "already-safe" string wrapper the way Kolon's
mark_rawor Mojo::ByteStream do -- stdlib ERB's<%=never auto-escapes, so there is nothing to opt out of. -
#materialize(value) ⇒ Object
JSX children captured by the adapter's buffer-slice capture resolve to a plain String already; a Proc is called and its result used (mirrors the Perl backends' CODE-ref materialisation for engines whose children-capture mechanism yields a callable).
-
#render_named(name, child_bf, vars) ⇒ Object
Render
<name>.erb(relative topath) withchild_bfbound asbfandvars(a Hash, symbol-keyed) bound asv.
Constructor Details
#initialize(path:, json_encoder: nil, cache: true) ⇒ Erb
ERB template locals contract (spec/compiler.md "ERB emission
contract"): every compiled template receives exactly two locals --
bf (the BarefootJS::Context for this render) and v (a Hash with
SYMBOL keys holding every prop/signal/memo/module-constant the
template references). This is enforced by binding bf and v as
local variables in a dedicated eval scope per render (see
render_named) rather than passing a generic binding, so a template
can never accidentally see Ruby method-local state.
cache: mirrors the Xslate/Kolon backend's cache => $DEV ? 0 : 1
constructor option: true (default, production) parses each .erb
file once and reuses the compiled ERB::Compiler output for the life
of the process; false (dev) re-reads and re-parses from disk on
every render_named call, so bf build --watch output is picked up
on the next request without restarting the server.
50 51 52 53 54 55 |
# File 'lib/barefoot_js/backend/erb.rb', line 50 def initialize(path:, json_encoder: nil, cache: true) @dir = path @json_encoder = json_encoder || ->(data) { JSON.generate(data) } @cache = {} @cache_enabled = cache end |
Instance Method Details
#encode_json(data) ⇒ Object
57 58 59 |
# File 'lib/barefoot_js/backend/erb.rb', line 57 def encode_json(data) @json_encoder.call(data) end |
#mark_raw(str) ⇒ Object
ERB has no "already-safe" string wrapper the way Kolon's mark_raw
or Mojo::ByteStream do -- stdlib ERB's <%= never auto-escapes, so
there is nothing to opt out of. Identity, kept only so runtime
helpers (spread_attrs) share one backend.mark_raw(...) call
shape across every BarefootJS backend port.
66 67 68 |
# File 'lib/barefoot_js/backend/erb.rb', line 66 def mark_raw(str) str end |
#materialize(value) ⇒ Object
JSX children captured by the adapter's buffer-slice capture resolve to a plain String already; a Proc is called and its result used (mirrors the Perl backends' CODE-ref materialisation for engines whose children-capture mechanism yields a callable).
74 75 76 |
# File 'lib/barefoot_js/backend/erb.rb', line 74 def materialize(value) value.respond_to?(:call) ? value.call : value end |
#render_named(name, child_bf, vars) ⇒ Object
Render <name>.erb (relative to path) with child_bf bound as
bf and vars (a Hash, symbol-keyed) bound as v.
80 81 82 83 |
# File 'lib/barefoot_js/backend/erb.rb', line 80 def render_named(name, child_bf, vars) template = load_template(name) Renderer.new(child_bf, vars || {}).render(template) end |