Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

The mustache template language in Go

License

NotificationsYou must be signed in to change notification settings

cbroglie/mustache

 
 

Repository files navigation

Build StatusGo DocGo Report CardcodecovDownloadsLatest release

logo


Why a Fork?

I forkedhoisie/mustache because it does not appear to be maintained, and I wanted to add the following functionality:

  • Update the API to follow the idiomatic Go convention of returning errors (this is a breaking change)
  • Add option to treat missing variables as errors

CLI Overview

~ go install github.com/cbroglie/mustache/cmd/mustache@latest➜~ mustacheUsage:  mustache [data] template [flags]Examples:  $ mustache data.yml template.mustache  $ cat data.yml| mustache template.mustache  $ mustache --layout wrapper.mustache data template.mustache  $ mustache --overide over.yml data.yml template.mustacheFlags:  -h, --helphelpfor mustache  --layout     a file to use as the layout template  --override   a data.yml file whose definitions supercede data.yml➜~

Package Overview

This library is an implementation of the Mustache template language in Go.

Mustache Spec Compliance

mustache/spec contains the formal standard for Mustache, and it is included as a submodule (using v1.2.1) for testing compliance. All of the tests pass (big thanks tokei10in), with the exception of the null interpolation tests added in v1.2.1. There is experimental support for a subset of the optional lambda functionality (thanks tofromhut). The optional inheritance functionality has not been implemented.


Documentation

For more information about mustache, check out themustache project page or themustache manual.

Also check out someexample mustache files.


Installation

To install the CLI, rungo install github.com/cbroglie/mustache/cmd/mustache@latest. To use it in a program, rungo get github.com/cbroglie/mustache and useimport "github.com/cbroglie/mustache".


Usage

There are four main methods in this package:

Render(datastring,context...interface{}) (string,error)RenderFile(filenamestring,context...interface{}) (string,error)ParseString(datastring) (*Template,error)ParseFile(filenamestring) (*Template,error)

There are also two additional methods for using layouts (explained below); as well as several more that can provide a custom Partial retrieval.

The Render method takes a string and a data source, which is generally a map or struct, and returns the output string. If the template file contains an error, the return value is a description of the error. There's a similar method, RenderFile, which takes a filename as an argument and uses that for the template contents.

data,err:=mustache.Render("hello {{c}}",map[string]string{"c":"world"})

If you're planning to render the same template multiple times, you do it efficiently by compiling the template first:

tmpl,_:=mustache.ParseString("hello {{c}}")varbuf bytes.Bufferfori:=0;i<10;i++ {tmpl.FRender(&buf,map[string]string{"c":"world"})}

For more example usage, please seemustache_test.go


Escaping

mustache.go follows the official mustache HTML escaping rules. That is, if you enclose a variable with two curly brackets,{{var}}, the contents are HTML-escaped. For instance, strings like5 > 2 are converted to5 &gt; 2. To use raw characters, use three curly brackets{{{var}}}.


Layouts

It is a common pattern to include a template file as a "wrapper" for other templates. The wrapper may include a header and a footer, for instance. Mustache.go supports this pattern with the following two methods:

RenderInLayout(datastring,layoutstring,context...interface{}) (string,error)RenderFileInLayout(filenamestring,layoutFilestring,context...interface{}) (string,error)

The layout file must have a variable called{{content}}. For example, given the following files:

layout.html.mustache:

<html><head><title>Hi</title></head><body>{{{content}}}</body></html>

template.html.mustache:

<h1>Hello World!</h1>

A call toRenderFileInLayout("template.html.mustache", "layout.html.mustache", nil) will produce:

<html><head><title>Hi</title></head><body><h1>Hello World!</h1></body></html>

Custom PartialProvider

Mustache.go has been extended to support a user-defined repository for mustache partials, instead of the default of requiring file-based templates.

Several new top-level functions have been introduced to take advantage of this:

funcRenderPartials(datastring,partialsPartialProvider,context...interface{}) (string,error)funcRenderInLayoutPartials(datastring,layoutDatastring,partialsPartialProvider,context...interface{}) (string,error)funcParseStringPartials(datastring,partialsPartialProvider) (*Template,error)funcParseFilePartials(filenamestring,partialsPartialProvider) (*Template,error)

APartialProvider is any object that responds toGet(string) (*Template,error), and two examples are provided- aFileProvider thatrecreates the old behavior (and is indeed used internally for backwardscompatibility); and aStaticProvider alias for amap[string]string. Usingeither of these is simple:

fp:=&FileProvider{Paths: []string{"","/opt/mustache","templates/" },Extensions: []string{"",".stache",".mustache" },}tmpl,err:=ParseStringPartials("This partial is loaded from a file: {{>foo}}",fp)sp:=StaticProvider(map[string]string{"foo":"{{>bar}}","bar":"some data",})tmpl,err:=ParseStringPartials("This partial is loaded from a map: {{>foo}}",sp)

A note about method receivers

Mustache.go supports calling methods on objects, but you have to be aware of Go's limitations. For example, lets's say you have the following type:

typePersonstruct {FirstNamestringLastNamestring}func (p*Person)Name1()string {returnp.FirstName+" "+p.LastName}func (pPerson)Name2()string {returnp.FirstName+" "+p.LastName}

While they appear to be identical methods,Name1 has a pointer receiver, andName2 has a value receiver. Objects of typePerson(non-pointer) can only accessName2, while objects of type*Person(person) can access both. This is by design in the Go language.

So if you write the following:

mustache.Render("{{Name1}}",Person{"John","Smith"})

It'll be blank. You either have to use&Person{"John", "Smith"}, or callName2

Supported features

  • Variables
  • Comments
  • Change delimiter
  • Sections (boolean, enumerable, and inverted)
  • Partials

About

The mustache template language in Go

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go99.2%
  • Other0.8%

[8]ページ先頭

©2009-2025 Movatter.jp