mustachio

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 7, 2025 License: MIT Imports: 6 Imported by: 0

README

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

Documentation

Overview

Package mustachio provides a fast, spec-compliant Mustache template engine for Go.

Mustachio implements the Mustache templating language specification (https://mustache.github.io/mustache.5.html) with support for:

  • Variables with HTML escaping: {{name}}
  • Unescaped variables: {{{name}}} and {{& name}}
  • Sections and inverted sections: {{#section}}...{{/section}}
  • Partials: {{> user}}
  • Set delimiters: {{=<% %>=}}
  • Lambda functions (optional spec feature)
  • Numeric indexing in dotted names

Example:

template := "Hello {{name}}!"
data := map[string]any{"name": "World"}
result, err := mustachio.Render(template, data, nil)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

func Parse(template string, delims delimiters) (*rootNode, error)

func Render

func Render(template string, data any, partials PartialLoader) (string, error)

Types

type MapPartials

type MapPartials map[string]string

func (MapPartials) LoadPartial

func (m MapPartials) LoadPartial(name string) (string, bool)

type MapProvider

type MapProvider struct {
	// contains filtered or unexported fields
}

func NewMapProvider

func NewMapProvider(root any) *MapProvider

func (*MapProvider) Lookup

func (p *MapProvider) Lookup(name string) (any, bool)

func (*MapProvider) Push

func (p *MapProvider) Push(ctx any) ValueProvider

type PartialLoader

type PartialLoader interface {
	LoadPartial(name string) (string, bool)
}

type ValueProvider

type ValueProvider interface {
	// Lookup returns a value for a given dotted name within the current stack of contexts.
	// If not found, returns (nil, false).
	Lookup(name string) (any, bool)
	// Push returns a new provider with an additional context pushed on top of the stack.
	Push(ctx any) ValueProvider
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL