fasttemplate

package
v0.32.7 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2024 License: Apache-2.0, MIT Imports: 5 Imported by: 0

README

fasttemplate

Simple and fast template engine for Go. Forked from fasttemplate.

This package was modified from the original one:

  1. usage of unsafe is removed
  2. usage of buffer pools is removed

Please note that fasttemplate doesn't do any escaping on template values unlike html/template do. So values must be properly escaped before passing them to fasttemplate.

Documentation

Overview

Package fasttemplate implements simple and fast template library.

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

Fasttemplate ideally fits for fast and simple placeholders' substitutions.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Execute

func Execute(template, startTag, endTag string, 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.

This function is optimized for constantly changing templates. Use Template.Execute for frozen templates.

func ExecuteString

func ExecuteString(template, startTag, endTag string, m map[string]interface{}) string

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 constantly changing templates. Use Template.ExecuteString for frozen templates.

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.

Example
template := "foo[baz]bar"
bazSlice := [][]byte{[]byte("123"), []byte("456"), []byte("789")}
m := map[string]interface{}{
	// Always wrap the function into TagFunc.
	//
	// "baz" tag function writes bazSlice contents into w.
	"baz": TagFunc(func(w io.Writer, tag string) (int, error) {
		var nn int
		for _, x := range bazSlice {
			n, err := w.Write(x)
			if err != nil {
				return nn, err
			}
			nn += n
		}
		return nn, nil
	}),
}

s := ExecuteString(template, "[", "]", m)
fmt.Printf("%s", s)
Output:

foo123456789bar

Jump to

Keyboard shortcuts

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