templates

package
v0.26.0 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2024 License: GPL-3.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(name string, funcs ...map[string]any) (tpl *template.Template)

New returns a new Template instance with configured funcs (including TemplateFuncs)

func TempDir

func TempDir() string

TempDir returns the volatile temporary directory that is returned by template function tempDir

func TempFile

func TempFile(name string) (filename string)

TempFile returns the volatile temporary file that is returned by template function tempFile

func TemplateFuncs

func TemplateFuncs(funcs ...map[string]any) (templateFuncs map[string]any)

TemplateFuncs declares a few standard functions to simplify working with templates

Available functions:

  • {{ "some string" | contains "some" }} => true
  • {{ "some string" | matches "^.+str.+$" }} => true
  • {{ "some old string" | replace "old" "new" }} => "some new string"
  • {{ "some old string" | replaceR "(old)" "$1 and new" }} => "some old and new string"
  • {{ "some old string" | regex "(old)" "$1 and new" }} => "some old and new string" (alias to "replaceR")
  • {{ "ABC" | lower }} => "abc"
  • {{ "abc" | upper }} => "ABC"
  • {{ " A " | trim }} => "A"
  • {{ "--A-" | trimPrefix " " }} => "A-"
  • {{ "--A-" | trimSuffix " " }} => "--A"
  • {{ "A,B,C" | split "," }} => ["A", "B", "C"]
  • {{ "A,B,C" | split "," | join ";" }} => "A;B;C"
  • {{ "A ,B, C" | splitR "\\s*,\\s*" | join ";" }} => "A;B;C"
  • {{ list "A" "B" "C" }} => ["A", "B", "C"]
  • {{ with $v := map "k1" "v1" "k2" "v2" }} {{ .k1 }}-{{ .k2 }} {{ end }} => " v1-v2 "
  • {{ with $v := list "A" "B" "C" "D" | map }} {{ ._0 }}-{{ ._1 }}-{{ ._3 }} {{ end }} => " A-B-D "
  • {{ with $v := list "A" "B" "C" "D" | map "key" }} {{ .key | join "-" }} {{ end }} => " A-B-C-D "
  • {{ "plain" | hex }} => "706c61696e"
  • {{ "plain" | base64 }} => "cGxhaW4="
  • {{ tempDir }} => "/path/to/unique-tempdir"
  • {{ tempFile "filename" }} => "/path/to/unique-tempdir/filename"

Types

type DefaultData

type DefaultData struct {
	Now        time.Time
	CurrentDir string
	StartupDir string
	TempDir    string
	BinaryDir  string
	Hostname   string
	OS         string
	Arch       string
	Env        map[string]string
}

DefaultData provides default variables for templates

func NewDefaultData

func NewDefaultData(env map[string]string) (data DefaultData)

NewDefaultData returns an initialized DefaultData

func (*DefaultData) InitDefaults

func (d *DefaultData) InitDefaults()

InitDefaults initializes DefaultData if not yet initialized

Jump to

Keyboard shortcuts

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