All Projects → alexkappa → Mustache

alexkappa / Mustache

Licence: mit
Mustache templating language in Go

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to Mustache

Errorstacks
Tiny library to parse error stack traces
Stars: ✭ 29 (-49.12%)
Mutual labels:  parsing
Logos
Create ridiculously fast Lexers
Stars: ✭ 1,001 (+1656.14%)
Mutual labels:  parsing
Elastalert Docker
Simple Dockerfile for building a Kubernetes and Elastalert Helm compatible Docker image.
Stars: ✭ 54 (-5.26%)
Mutual labels:  mustache
Pomerium Helm
Official helm charts for Pomerium.
Stars: ✭ 32 (-43.86%)
Mutual labels:  mustache
Jasper
(alpha, do not use) -- A programming language, meant to be a simpler and nicer Javascript.
Stars: ✭ 38 (-33.33%)
Mutual labels:  parsing
Pyparsing
Python library for creating PEG parsers
Stars: ✭ 1,052 (+1745.61%)
Mutual labels:  parsing
Comby
A tool for structural code search and replace that supports ~every language.
Stars: ✭ 912 (+1500%)
Mutual labels:  parsing
Lazyjson
A very fast, very lazy JSON parser for Java.
Stars: ✭ 55 (-3.51%)
Mutual labels:  parsing
Tox
misc parsers in rust
Stars: ✭ 40 (-29.82%)
Mutual labels:  parsing
Creditcard
Creditcard number parsing, validation and information extraction
Stars: ✭ 52 (-8.77%)
Mutual labels:  parsing
Helm Charts
Prometheus community Helm charts
Stars: ✭ 962 (+1587.72%)
Mutual labels:  mustache
K8s Minecraft
Running a Minecraft server in Kubernetes
Stars: ✭ 37 (-35.09%)
Mutual labels:  mustache
Plaso
Super timeline all the things
Stars: ✭ 1,055 (+1750.88%)
Mutual labels:  parsing
Domainname Parser
🏬 .NET domain name parsing library (uses publicsuffix.org)
Stars: ✭ 31 (-45.61%)
Mutual labels:  parsing
Commons Ip Math
Stars: ✭ 54 (-5.26%)
Mutual labels:  parsing
Got Reload
Reload Go code in a running process at function/method level granularity, using Yaegi
Stars: ✭ 29 (-49.12%)
Mutual labels:  parsing
Lug
Parsing expression grammar (PEG) embedded domain specific language and parsing machine for C++17
Stars: ✭ 44 (-22.81%)
Mutual labels:  parsing
Connect Api Specification
This repository contains the OpenAPI specification as well as templates for generating SDKs for Square's APIs
Stars: ✭ 56 (-1.75%)
Mutual labels:  mustache
Charts
Community managed Helm charts for running Falco with Kubernetes
Stars: ✭ 55 (-3.51%)
Mutual labels:  mustache
Python Tutorial Notebooks
Python tutorials as Jupyter Notebooks for NLP, ML, AI
Stars: ✭ 52 (-8.77%)
Mutual labels:  parsing

Mustache

godoc reference wercker status Code Climate Test Coverage

This is an implementation of the mustache templating language in Go.

It is inspired by hoisie/mustache however it's not a fork, rather a re-implementation with improved spec conformance, a more flexible API (e.g. support for io.Writer and io.Reader).

It is built using lexing techniques described in the slides on lexical scanning in Go, and functional options as described in the blog post on self-referential functions and the design of options.

This package aims to cover 100% of the mustache specification tests, however by the time of this writing it is not complete.

For more information on mustache check the official documentation and the mustache spec.

Installation

Install with go get github.com/alexkappa/mustache.

Documentation

The API documentation is available at godoc.org.

Usage

The core of this package is the Template, and it's Parse and Render functions.

template := mustache.New()
template.Parse(strings.NewReader("Hello, {{subject}}!"))
template.Render(os.Stdout, map[string]string{"subject": "world"})

Helpers

There are additional Parse and Render helpers to deal with different kind of input or output, such as string, []byte or io.Writer/io.Reader.

Parse(r io.Reader) error
ParseString(s string) error
ParseBytes(b []byte) error
Render(w io.Writer, context interface{}) error
RenderString(context interface{}) (string, error)
RenderBytes(context interface{}) ([]byte, error)

Reader/Writer

f, err := os.Open("template.mustache")
if err != nil {
    fmt.Fprintf(os.Stderr, "failed to open file: %s\n", err)
}
t, err := Parse(f)
if err != nil {
    fmt.Fprintf(os.Stderr, "failed to parse template: %s\n", err)
}
t.Render(os.Stdout, nil)

Note: in the example above, we used Parse which wraps the t := New() and t.Parse() functions for consiceness.

String

t := mustache.New()
err := t.ParseString("Hello, {{subject}}!")
if err != nil {
    // handle error
}
s, _ := t.RenderString(map[string]string{"subject": "world"})
if err != nil {
    // handle error
}
fmt.Println(s)

Options

It is possible to define some options on the template, which will alter the way the template will parse, render or fail.

The options are:

  • Name(n string) Option sets the name of the template. This option is useful when using the template as a partial to another template.
  • Delimiters(start, end string) Option sets the start and end delimiters of the template.
  • Partial(p *Template) Option sets p as a partial to the template. It is important to set the name of p so that it may be looked up by the parent template.
  • SilentMiss(silent bool) Option sets missing variable lookup behaviour.

Options can be defined either as arguments to New or using the Option function.

Partials

Partials are templates themselves and can be defined using the Partial option.

Note: It is important to name the partial using the Name option which should match the mustache partial tag {{>name}} in the parent template.

title := New(
    Name("header")        // instantiate and name the template
    Delimiters("|", "|")) // set the mustache delimiters to | instead of {{

title.ParseString("|title|") // parse a template string

body := New()
body.Option(Name("body"))
body.ParseString("{{content}}")

template := New(
    SilentMiss(false), // return an error if a variable lookup fails
    Partial(title),    // register a partial
    Partial(body))     // and another one...

template.ParseString("{{>header}}\n{{>body}}")

context := map[string]interface{}{
    "title":   "Mustache",
    "content": "Logic less templates with Mustache!",
}

template.Render(os.Stdout, context)

Tests

Run go test as usual. If you want to run the spec tests against this package, make sure you've checked out the specs submodule. Otherwise spec tests will be skipped.

Currently certain spec tests are skipped as they fail due to an issue with how standalone tags and empty lines are being handled. Inspecting them manually, one can see that the templates render correctly but with some additional \n which should have been omited. See issue #1.

See SPEC.md for a breakdown of which spec tests pass and fail.

Contributing

If you would like to contribute, head on to the issues page for tasks that need help.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].