fasttemplate

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2021 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package fasttemplate implements simple and fast template library. Credits goes to: https://github.com/valyala/fasttemplate

Fasttemplate is faster than text/template, strings.Replace and strings.Replacer.

Fasttemplate ideally fits for fast and simple placeholders' substitutions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TagFunc

type TagFunc func(w io.Writer, tag string) (int, error)

TagFunc can be used as a substitution value in the map passed to Execute*. Execute* functions pass tag (placeholder) name in 'tag' argument.

TagFunc must be safe to call from concurrently running goroutines.

TagFunc must write contents to w and return the number of bytes written.

type Template

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

Template implements simple template engine, which can be used for fast tags' (aka placeholders) substitution.

func NewTemplate

func NewTemplate(template, startTag, endTag string) (*Template, error)

NewTemplate parses the given template using the given startTag and endTag as tag start and tag end.

The returned template can be executed by concurrently running goroutines using Execute* methods.

func (*Template) Execute

func (t *Template) Execute(w io.Writer, m map[string]interface{}) (int64, error)

Execute substitutes template tags (placeholders) with the corresponding values from the map m and writes the result to the given writer w.

Substitution map m may contain values with the following types:

  • []byte - the fastest value type
  • string - convenient value type
  • TagFunc - flexible value type

Returns the number of bytes written to w.

func (*Template) ExecuteFunc

func (t *Template) ExecuteFunc(w io.Writer, f TagFunc) (int64, error)

ExecuteFunc calls f on each template tag (placeholder) occurrence.

Returns the number of bytes written to w.

This function is optimized for frozen templates. Use ExecuteFunc for constantly changing templates.

func (*Template) ExecuteFuncString

func (t *Template) ExecuteFuncString(f TagFunc) []byte

ExecuteFuncString calls f on each template tag (placeholder) occurrence and substitutes it with the data written to TagFunc's w.

Returns the resulting string.

This function is optimized for frozen templates. Use ExecuteFuncString for constantly changing templates.

func (*Template) ExecuteString

func (t *Template) ExecuteString(m map[string]interface{}) []byte

ExecuteString substitutes template tags (placeholders) with the corresponding values from the map m and returns the result.

Substitution map m may contain values with the following types:

  • []byte - the fastest value type
  • string - convenient value type
  • TagFunc - flexible value type

This function is optimized for frozen templates. Use ExecuteString for constantly changing templates.

func (*Template) Reset

func (t *Template) Reset(template, startTag, endTag string) error

Reset resets the template t to new one defined by template, startTag and endTag.

Reset allows Template object re-use.

Reset may be called only if no other goroutines call t methods at the moment.

Jump to

Keyboard shortcuts

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