Documentation
¶
Overview ¶
Package haml is a pure-Go (no cgo) reimplementation of the Ruby Haml template engine (the `haml` gem) — the deterministic, interpreter-independent core that turns an indentation-structured Haml template into the Ruby source that, when evaluated against a set of locals, renders the same HTML the gem produces.
It mirrors the go-ruby-erb design: this package COMPILES a template to Ruby source; the final eval that runs any embedded Ruby (`=` expressions, `-` control, `#{}` interpolation, dynamic attribute hashes) needs a Ruby interpreter and is deliberately left to the consumer (go-embedded-ruby/rbgo).
Everything that is static — element structure, `.class`/`#id` shorthand, literal attribute hashes, the doctype, comments, and the `:plain`/`:css`/ `:javascript`/`:escaped`/`:preserve` filters — is resolved at compile time into literal HTML runs, so a template with no embedded Ruby renders with no interpreter at all. That deterministic core is what the ruby-free tests cover to 100%.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Compile ¶
Compile compiles a Haml template into the Ruby source that, when eval'd with the template's locals in scope, builds and returns the rendered HTML string — matching the `haml` gem's rendered output. err is non-nil only for genuinely malformed templates; well-formed templates always compile (the compiled Ruby may still raise at eval time, which is the host's concern).
The returned source assigns the buffer to BufVar, appends every fragment, and evaluates to the buffer as its final expression, so a host renders with `eval(src)` after binding the locals.
func HTMLEscape ¶
HTMLEscape replaces the five HTML-significant characters with their entity references, matching Haml::Util.escape_html exactly (note "'" becomes "'"). It is exposed so a host embedding the compiled source can provide the runtime escape helper the emitted Ruby calls.
func Render ¶
Render compiles template and renders it via eval, using the supplied evaluator. It is the compile+eval seam that mirrors go-ruby-erb.Render: the pure-Go side compiles to Ruby source and hands it, together with the locals, to eval — a pluggable function a host such as rbgo provides. When eval is nil, Render returns the compiled source unchanged as a convenience for callers that only need the source.
locals maps a local-variable name to the Ruby literal source for its value (e.g. "name" -> `"World"`); the evaluator is expected to bind them before eval'ing the compiled source.
func TrimTrailingNewline ¶
TrimTrailingNewline reports whether s ends in a newline; a small helper used by hosts that want to normalise the buffer. It is part of the public surface so the rendering contract (Haml always emits a trailing newline per line) is documented and testable.
Types ¶
type Evaluator ¶
Evaluator is the Ruby-eval seam: given compiled Ruby source and the locals to bind, it returns the rendered string. go-embedded-ruby/rbgo supplies a real implementation; the pure-Go tests use a tiny deterministic shim for the interpreter-free cases.
type Options ¶
type Options struct {
// BufVar names the output-buffer local variable the compiled source appends
// to. When empty it defaults to "_hamlout".
BufVar string
// EscapeFn names the Ruby method the compiled source calls to HTML-escape an
// interpolated `=` expression. When empty it defaults to
// "::Haml::Util.escape_html", the helper the gem uses and that the host
// (rbgo) provides at eval time.
EscapeFn string
// Format selects the output format, mirroring the gem's :format option: it
// governs the doctype table, void/self-closing tag rendering (html5 "<br>"
// vs xhtml "<br />") and boolean-attribute rendering (html5 bare "checked"
// vs xhtml `checked="checked"`). Valid values are "html5" (the default when
// empty, matching the gem), "xhtml" and "html4".
Format string
}
Options configures Compile.
type SyntaxError ¶
SyntaxError reports a malformed Haml template, mirroring the gem's Haml::SyntaxError. Line carries the offending source line.
func (*SyntaxError) Error ¶
func (e *SyntaxError) Error() string
