Skip to content

weese/mustachio

Repository files navigation

mustachio

⚡ A fast, readable Mustache template engine for Go

Features

  • 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
  • Extensions implemented
    • λ Lambdas (optional per spec)
      • Variable lambdas: func() string
      • Section lambdas: func(string) string and func(string, func(string) string) string (render callback)
    • Numeric indexing in dotted names (e.g., track.0.artist.#text)
  • Testing
    • Unit tests for core features and lambdas
    • Spec runner executes JSON fixtures from spec/specs/*.json

Not (yet) implemented

  • Inheritance (Blocks/Parents): {{$block}} / {{<parent}} (optional module)
  • Dynamic names (optional module)

Install

go get github.com/weese/mustachio@latest

Usage

Render 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>

Running tests

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).

API

  • Render(template string, data any, partials PartialLoader) (string, error)
    • data: typically map[string]any, but any Go value is accepted and used as the root context
    • partials: implement PartialLoader or use mustachio.MapPartials

License

MIT

References

About

A fast, readable Mustache template engine for Go

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages