Documentation
¶
Overview ¶
Package mustache is a pure-Go (CGO-free) implementation of Ruby's logic-less Mustache templating — the `mustache` gem's rendering engine — faithful to the language-independent mustache spec. It compiles a template to a small tree of tokens and renders it against a context drawn from the Ruby value model, so a host (such as go-embedded-ruby) can render Mustache templates against its own object graph without any Ruby runtime.
Ruby value model ¶
A context value is an [any] drawn from a small, fixed set of Go types so a host can map its own object graph to and from this package:
Ruby Go ---- -- nil nil true / false bool Integer int, int64, *big.Int Float float64, float32 String string Symbol Symbol (a Hash key spelled :name) Array []any Hash map[string]any, map[Symbol]any, *Map (ordered) Lambda / proc Lambda, func() any, func(string) any Object Object (an interface exposing named methods)
A Ruby Hash keyed by symbols is common in Mustache data; a plain map[string]any and a map[Symbol]any are both accepted and a name resolves against either. Everything else is coerced to a string with ToString before interpolation, matching Ruby's `to_s`.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Render ¶
Render compiles template and renders it against context, resolving `{{>name}}` partials from the partials map (a nil or absent partial renders as the empty string). It is the package's core entry point, equivalent to the gem's `Mustache.render(template, context)` with an explicit partial set.
context is a value from the package value model — typically a map[string]any / map[Symbol]any / *Map (a Ruby Hash), but any Value is accepted and pushed as the sole context frame. partials maps a partial name to its template source.
func RenderString ¶
RenderString renders template against context with no partials — the common one-shot form of the gem's `Mustache.render(template, context)`.
Types ¶
type Lambda ¶
Lambda is a Mustache lambda. A section lambda receives the unrendered section body and returns a value rendered against the current delimiters; an interpolation lambda ignores the argument (Section is empty) and returns a value rendered against the default delimiters. Returning a string is the common case; any Value is accepted and coerced.
The func() any and func(string) any Go shapes are also accepted directly as context values and adapted to this type, so a caller need not wrap a plain closure.
type Map ¶
type Map struct {
// contains filtered or unexported fields
}
Map is an insertion-ordered Ruby Hash accepted as a context value. A name lookup matches a key stored as a string or a Symbol.
type Mustache ¶
type Mustache struct {
// Template is the template source. When empty, Render renders nothing.
Template string
// Context is the primary view data — typically a map[string]any /
// map[Symbol]any / *Map (a Ruby Hash). It is the bottom of the context stack.
Context Value
// View, when non-nil, is consulted for names not found in Context, modelling a
// Mustache subclass's view methods. It sits below Context on the stack so
// explicit data overrides a method of the same name (Ruby's precedence).
View Object
// Partials maps a partial name to its template source.
Partials map[string]string
}
Mustache is the gem's class-based view API. It pairs a template with a view — a set of named values (a Hash) and/or an Object exposing methods — plus a partial set, mirroring a `Mustache` subclass instance. Zero value is usable: set Template and (optionally) Context / View / Partials, then call Render.
type Object ¶
Object is a Ruby object exposing named "methods" to a template — a Mustache view. Method returns the value for the named method and whether the object responds to it. A host binds its own instances behind this interface so `{{name}}` and `{{#name}}` resolve to method calls.
