- Core Mustache
- Variables with HTML escaping:
{{name}} - Unescaped variables:
{{{name}}}and{{& name}} - Dotted names and context precedence:
{{a.b.c}} - Implicit iterator:
{{.}} - Sections and inverted sections:
{{#section}}...{{/section}},{{^section}}...{{/section}} - Lists and nested contexts
- Partials:
{{> user}}with indentation handling for standalone usage - Set delimiters:
{{=<% %>=}} ... <%={{ }}=%} - Standalone trimming (sections, inverted, partials, comments, set-delims)
- CR and CRLF line ending handling in standalone detection
- Variables with HTML escaping:
- Extensions implemented
- λ Lambdas (optional per spec)
- Variable lambdas:
func() string - Section lambdas:
func(string) stringandfunc(string, func(string) string) string(render callback)
- Variable lambdas:
- Numeric indexing in dotted names (e.g.,
track.0.artist.#text)
- λ Lambdas (optional per spec)
- Testing
- Unit tests for core features and lambdas
- Spec runner executes JSON fixtures from
spec/specs/*.json
- Inheritance (Blocks/Parents):
{{$block}}/{{<parent}}(optional module) - Dynamic names (optional module)
go get github.com/weese/mustachio@latestRender a simple template with data:
package main
import (
"fmt"
"github.com/weese/mustachio"
)
func main() {
tpl := "Hello {{name}}!"
out, err := mustachio.Render(tpl, map[string]any{"name": "World"}, nil)
if err != nil { panic(err) }
fmt.Println(out) // Hello World!
}Render with partials, sections, and lambdas:
partials := mustachio.MapPartials{
"user": "<strong>{{name}}</strong>",
}
data := map[string]any{
"name": "Chris",
"wrapped": func(text string, render func(string) string) string {
return "<b>" + render(text) + "</b>"
},
}
tpl := "{{#wrapped}}Hi {{> user}}{{/wrapped}}"
out, err := mustachio.Render(tpl, data, partials)
// out => <b>Hi <strong>Chris</strong></b>The repo includes unit tests and spec tests. Spec tests test against the official spec fixtures. They require the spec submodule to be present:
git submodule update --init --recursive
go test ./...The spec runner automatically loads all spec/specs/*.json files (excluding optional modules and inheritance by default).
Render(template string, data any, partials PartialLoader) (string, error)data: typicallymap[string]any, but any Go value is accepted and used as the root contextpartials: implementPartialLoaderor usemustachio.MapPartials
MIT
- Spec reference: mustache(5) manual
- Official spec tests: mustache/spec
- Original Ruby implementation: mustache/mustache