Documentation
¶
Overview ¶
Package templates provides utility functions for working with Go templates.
Index ¶
- Variables
- func Chain2[T any](f Func2[T]) func(T, ...reflect.Value) (reflect.Value, error)
- func Chain3[T1, T2 any](f Func3[T1, T2]) func(T1, T2, ...reflect.Value) (reflect.Value, error)
- func Chain4[T1, T2, T3 any](f Func4[T1, T2, T3]) func(T1, T2, T3, ...reflect.Value) (reflect.Value, error)
- func Execute(name, content string, data any, options ...string) (string, error)
- func New(name string) *template.Template
- type Any
- type Bool
- type Dictionary
- type Func
- type Func2
- type Func3
- type Func4
- type FuncChain
- type Int
- type Number
- type Slice
- type SliceOrString
- type String
- type Vector
- func (v *Vector) Clear()
- func (v Vector) Get(index int) (any, error)
- func (v *Vector) Insert(index int, item any) error
- func (v Vector) IsEmpty() bool
- func (v Vector) Len() int
- func (v *Vector) Pop() (any, error)
- func (v *Vector) Push(item any) Vector
- func (v *Vector) RemoveAt(index int) error
- func (v Vector) Set(index int, item any) error
- func (v *Vector) Splice(start, count int, items ...any) error
Constants ¶
This section is empty.
Variables ¶
var Funcs = template.FuncMap{ "_": func(...any) string { return "" }, "linespace": Chain(stringFunc("linespace", noError(linespace))), "fields": Chain(stringFunc("fields", noError(strings.Fields))), "quote": Chain(stringFunc("quote", noError(strconv.Quote))), "unquote": Chain(stringFunc("unquote", strconv.Unquote)), "capitalize": Chain(stringFunc("capitalize", noError(stringutil.Capitalize))), "lower": Chain(stringFunc("lower", noError(strings.ToLower))), "upper": Chain(stringFunc("upper", noError(strings.ToUpper))), "replace": Chain3(replace), "replaceN": Chain4(replaceN), "trim": Chain(stringFunc("trim", noError(strings.TrimSpace))), "trimPrefix": Chain2(trimPrefix), "hasPrefix": Chain2(hasPrefix), "trimSuffix": Chain2(trimSuffix), "hasSuffix": Chain2(hasSuffix), "split": Chain2(split), "join": Chain2(join), "striptags": Chain(stringFunc("striptags", striptags)), "substr": Chain3(substr), "repeat": Chain2(repeat), "camelCase": Chain(stringFunc("camelCase", noError(stringutil.CamelCase))), "pascalCase": Chain(stringFunc("pascalCase", noError(stringutil.PascalCase))), "snakeCase": Chain(stringFunc("snakeCase", noError(stringutil.SnakeCase))), "kebabCase": Chain(stringFunc("kebabCase", noError(stringutil.KebabCase))), "truncate": Chain3(truncate), "wordwrap": Chain2(wordwrap), "center": Chain2(center), "matchRegex": Chain2(matchRegex), "html": Chain(stringFunc("html", noError(html.EscapeString))), "urlEscape": Chain(stringFunc("urlEscape", noError(url.QueryEscape))), "urlUnescape": Chain(stringFunc("urlUnescape", url.QueryUnescape)), "b64enc": Chain(stringFunc("b64enc", noError(b64enc))), "b64dec": Chain(stringFunc("b64dec", b64dec)), "list": list, "dict": dict, "map": Chain2(Map), "first": Chain(first), "last": Chain(last), "reverse": Chain(reverse), "sort": Chain(sortSlice), "uniq": Chain(uniq), "includes": Chain2(includes), "add": Chain2(add), "sub": Chain2(sub), "mul": Chain2(mul), "quo": Chain2(quo), "rem": Chain2(rem), "mod": Chain2(mod), "ceil": Chain(ceil), "floor": Chain(floor), "round": Chain2(round), "min": minFunc, "max": maxFunc, "int": Chain(toInt64), "float": Chain(toFloat64), "string": Chain(toString), "bool": Chain(toBool), "now": time.Now, "parseTime": parseTime, "joinPath": joinPath, "splitPath": Chain(splitPath), "absPath": Chain(stringFunc("absPath", absPath)), "relPath": Chain2(stringFunc2("relPath", relPath)), "cleanPath": Chain(stringFunc("cleanPath", noError(cleanPath))), "basename": Chain(stringFunc("basename", noError(basename))), "dirname": Chain(stringFunc("dirname", noError(dirname))), "extname": Chain(stringFunc("extname", noError(extname))), "removeExt": Chain(stringFunc("removeExt", noError(removeExt))), "isAbs": Chain(stringFunc("isAbs", noError(isAbs))), "glob": Chain(stringFunc("glob", glob)), "matchPath": Chain2(stringFunc2("matchPath", matchPath)), }
Funcs is a map of utility functions for use in templates
Functions ¶
func Chain4 ¶
func Chain4[T1, T2, T3 any](f Func4[T1, T2, T3]) func(T1, T2, T3, ...reflect.Value) (reflect.Value, error)
Chain4 returns a FuncChain that chains the given function.
Types ¶
type Dictionary ¶
@api(Container/Dictionary) represents a dictionary of any key-value pairs.
func (Dictionary) Clear ¶
func (d Dictionary) Clear()
@api(Container/Dictionary.Clear) removes all key-value pairs from the dictionary.
func (Dictionary) Get ¶
func (d Dictionary) Get(key any) any
@api(Container/Dictionary.Get) returns the value for the specified key.
func (Dictionary) Has ¶
func (d Dictionary) Has(key any) bool
@api(Container/Dictionary.Has) reports whether the dictionary has the specified key.
func (Dictionary) Remove ¶
func (d Dictionary) Remove(key any)
@api(Container/Dictionary.Remove) removes the specified key from the dictionary.
func (Dictionary) Set ¶
func (d Dictionary) Set(key, value any)
@api(Container/Dictionary.Set) sets the value for the specified key.
type Func3 ¶
Func3 is a function that converts a T1, T2 and a reflect.Value to another reflect.Value.
type Func4 ¶
Func4 is a function that converts a T1, T2, T3 and a reflect.Value to another reflect.Value.
type FuncChain ¶
FuncChain is a function chain that can be used to convert values.
The function must accept 0 or 1 arguments.
If the function accepts 0 arguments, it returns itself (the function). If the function accepts 1 argument and the argument is a FuncChain, it returns a new FuncChain that chains the two functions. Otherwise, it returns the result of calling the function with the argument.
type SliceOrString ¶
SliceOrString is a type alias for reflect.Value. It's used to mark a value as a slice or a string.
type Vector ¶
type Vector []any
@api(Container/Vector) represents a slice of any type. Function `list` creates a Vector from the given values.
Example: ```tmpl {{list 1 2 3}} ```
Output: ``` [1 2 3] ```
func (*Vector) Clear ¶
func (v *Vector) Clear()
@api(Container/Vector.Clear) removes all items from the vector.
func (Vector) IsEmpty ¶
@api(Container/Vector.IsEmpty) reports whether the vector is empty.
Example: ```tmpl {{- $v := list 1 2}} {{- $v.IsEmpty}} {{- $v.Pop | _ }} {{- $v.IsEmpty}} {{- $v.Pop | _ }} {{- $v.IsEmpty}} ```
Output: ``` false false true ```
func (Vector) Len ¶
@api(Container/Vector.Len) returns the length of the vector.
Example: ```tmpl {{- $v := list 1 2 3 4 5}} {{- $v.Len}} ```
Output: ``` 5 ```
func (*Vector) Pop ¶
@api(Container/Vector.Pop) pops an item from the end of the vector. It returns an error if the vector is empty.
Example: ```tmpl {{- $v := list 1 2 3}} {{- $v.Pop}} {{$v}} ```
Output: ``` 3 [1 2] ```
func (*Vector) Push ¶
@api(Container/Vector.Push) pushes an item to the end of the vector.
Example: ```tmpl {{- $v := list 1 2 3}} {{$v.Push 4}} ```
Output: ``` [1 2 3 4] ```