mustache

package
v0.0.0-...-85711f0 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2021 License: Apache-2.0, MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SkipWhitespaceTagTypes = "#^/<>=!"
)

Skip all whitespaces apeared after these types of tags until end of line if the line only contains a tag and whitespaces.

Variables

View Source
var (
	// AllowMissingVariables defines the behavior for a variable "miss." If it
	// is true (the default), an empty string is emitted. If it is false, an error
	// is generated instead.
	AllowMissingVariables = true
)

Functions

func Render

func Render(data string, context ...interface{}) (string, error)

Render compiles a mustache template string and uses the the given data source - generally a map or struct - to render the template and return the output.

func RenderFile

func RenderFile(filename string, context ...interface{}) (string, error)

RenderFile loads a mustache template string from a file and compiles it, and then uses the the given data source - generally a map or struct - to render the template and return the output.

func RenderFileInLayout

func RenderFileInLayout(filename string, layoutFile string, context ...interface{}) (string, error)

RenderFileInLayout loads a mustache template string and layout "wrapper" template string from files and compiles them, and then uses the the given data source - generally a map or struct - to render the compiled templates and return the output.

func RenderInLayout

func RenderInLayout(data string, layoutData string, context ...interface{}) (string, error)

RenderInLayout compiles a mustache template string and layout "wrapper" and uses the given data source - generally a map or struct - to render the compiled templates and return the output.

func RenderInLayoutPartials

func RenderInLayoutPartials(data string, layoutData string, partials PartialProvider, context ...interface{}) (string, error)

func RenderPartials

func RenderPartials(data string, partials PartialProvider, context ...interface{}) (string, error)

RenderPartials compiles a mustache template string and uses the the given partial provider and data source - generally a map or struct - to render the template and return the output.

func RenderPartialsRaw

func RenderPartialsRaw(data string, partials PartialProvider, forceRaw bool, context ...interface{}) (string, error)

func RenderRaw

func RenderRaw(data string, forceRaw bool, context ...interface{}) (string, error)

Types

type FileProvider

type FileProvider struct {
	Paths      []string
	Extensions []string
}

FileProvider implements the PartialProvider interface by providing partials drawn from a filesystem. When a partial named `NAME` is requested, FileProvider searches each listed path for a file named as `NAME` followed by any of the listed extensions. The default for `Paths` is to search the current working directory. The default for `Extensions` is to examine, in order, no extension; then ".mustache"; then ".stache".

func (*FileProvider) Get

func (fp *FileProvider) Get(name string) (string, error)

type PartialProvider

type PartialProvider interface {
	// Get accepts the name of a partial and returns the parsed partial, if it could be found; a valid but empty
	// template, if it could not be found; or nil and error if an error occurred (other than an inability to find
	// the partial).
	Get(name string) (string, error)
}

PartialProvider comprises the behaviors required of a struct to be able to provide partials to the mustache rendering engine.

type StaticProvider

type StaticProvider struct {
	Partials map[string]string
}

StaticProvider implements the PartialProvider interface by providing partials drawn from a map, which maps partial name to template contents.

func (*StaticProvider) Get

func (sp *StaticProvider) Get(name string) (string, error)

type Tag

type Tag interface {
	// Type returns the type of the tag.
	Type() TagType
	// Name returns the name of the tag.
	Name() string
	// Tags returns any child tags. It panics for tag types which cannot contain
	// child tags (i.e. variable tags).
	Tags() []Tag
}

Tag represents the different mustache tag types.

Not all methods apply to all kinds of tags. Restrictions, if any, are noted in the documentation for each method. Use the Type method to find out the type of tag before calling type-specific methods. Calling a method inappropriate to the type of tag causes a run time panic.

type TagType

type TagType uint

A TagType represents the specific type of mustache tag that a Tag represents. The zero TagType is not a valid type.

const (
	Invalid TagType = iota
	Variable
	Section
	InvertedSection
	Partial
)

Defines representing the possible Tag types

func (TagType) String

func (t TagType) String() string

type Template

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

Template represents a compilde mustache template

func ParseFile

func ParseFile(filename string) (*Template, error)

ParseFile loads a mustache template string from a file and compiles it. The resulting output can be used to efficiently render the template multiple times with different data sources.

func ParseFilePartials

func ParseFilePartials(filename string, partials PartialProvider) (*Template, error)

ParseFilePartials loads a mustache template string from a file, retrieving any required partials from the given provider, and compiles it. The resulting output can be used to efficiently render the template multiple times with different data sources.

func ParseFilePartialsRaw

func ParseFilePartialsRaw(filename string, forceRaw bool, partials PartialProvider) (*Template, error)

func ParseString

func ParseString(data string) (*Template, error)

ParseString compiles a mustache template string. The resulting output can be used to efficiently render the template multiple times with different data sources.

func ParseStringPartials

func ParseStringPartials(data string, partials PartialProvider) (*Template, error)

ParseStringPartials compiles a mustache template string, retrieving any required partials from the given provider. The resulting output can be used to efficiently render the template multiple times with different data sources.

func ParseStringPartialsRaw

func ParseStringPartialsRaw(data string, partials PartialProvider, forceRaw bool) (*Template, error)

func ParseStringRaw

func ParseStringRaw(data string, forceRaw bool) (*Template, error)

func (*Template) FRender

func (tmpl *Template) FRender(out io.Writer, context ...interface{}) error

FRender uses the given data source - generally a map or struct - to render the compiled template to an io.Writer.

func (*Template) FRenderInLayout

func (tmpl *Template) FRenderInLayout(out io.Writer, layout *Template, context ...interface{}) error

FRenderInLayout uses the given data source - generally a map or struct - to render the compiled templated a loayout "wrapper" template to an io.Writer.

func (*Template) Render

func (tmpl *Template) Render(context ...interface{}) (string, error)

Render uses the given data source - generally a map or struct - to render the compiled template and return the output.

func (*Template) RenderInLayout

func (tmpl *Template) RenderInLayout(layout *Template, context ...interface{}) (string, error)

RenderInLayout uses the given data source - generally a map or struct - to render the compiled template and layout "wrapper" template and return the output.

func (*Template) Tags

func (tmpl *Template) Tags() []Tag

Tags returns the mustache tags for the given template

Jump to

Keyboard shortcuts

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