templates

package
v0.0.0-...-89aa834 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2024 License: MIT Imports: 18 Imported by: 2

Documentation

Overview

Package templates provides utility functions for working with Go templates.

Index

Constants

This section is empty.

Variables

View Source
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 Chain2

func Chain2[T any](f Func2[T]) func(T, ...reflect.Value) (reflect.Value, error)

Chain2 returns a FuncChain that chains the given function.

func Chain3

func Chain3[T1, T2 any](f Func3[T1, T2]) func(T1, T2, ...reflect.Value) (reflect.Value, error)

Chain3 returns a FuncChain that chains the given function.

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.

func Execute

func Execute(name, content string, data any, options ...string) (string, error)

Execute executes the default template with the given text and data.

func New

func New(name string) *template.Template

New creates a new template with the default functions.

Types

type Any

type Any = reflect.Value

Any is a type alias for reflect.Value. It's used to mark a value as any type.

type Bool

type Bool = reflect.Value

Bool is a type alias for reflect.Value. It's used to mark a value as a boolean.

type Dictionary

type Dictionary map[any]any

@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 Func

type Func func(reflect.Value) (reflect.Value, error)

Func is a function that converts a reflect.Value to another reflect.Value.

type Func2

type Func2[T any] func(T, reflect.Value) (reflect.Value, error)

Func2 is a function that converts a T and a reflect.Value to another reflect.Value.

type Func3

type Func3[T1, T2 any] func(T1, T2, reflect.Value) (reflect.Value, error)

Func3 is a function that converts a T1, T2 and a reflect.Value to another reflect.Value.

type Func4

type Func4[T1, T2, T3 any] func(T1, T2, T3, reflect.Value) (reflect.Value, error)

Func4 is a function that converts a T1, T2, T3 and a reflect.Value to another reflect.Value.

type FuncChain

type FuncChain func(...reflect.Value) (reflect.Value, error)

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.

func Chain

func Chain(f Func) FuncChain

Chain returns a FuncChain that chains the given function.

type Int

type Int = reflect.Value

Int is a type alias for reflect.Value. It's used to mark a value as an integer.

type Number

type Number = reflect.Value

Number is a type alias for reflect.Value. It's used to mark a value as a number.

type Slice

type Slice = reflect.Value

Slice is a type alias for reflect.Value. It's used to mark a value as a slice.

func Map

func Map(f FuncChain, v Slice) (Slice, error)

Map maps the given value to a slice of values using the given function chain.

type SliceOrString

type SliceOrString = reflect.Value

SliceOrString is a type alias for reflect.Value. It's used to mark a value as a slice or a string.

type String

type String = reflect.Value

String is a type alias for reflect.Value. It's used to mark a value as 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) Get

func (v Vector) Get(index int) (any, error)

@api(Container/Vector.Get) returns the item at the specified index.

func (*Vector) Insert

func (v *Vector) Insert(index int, item any) error

@api(Container/Vector.Insert) inserts an item at the specified index.

func (Vector) IsEmpty

func (v Vector) IsEmpty() bool

@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

func (v Vector) Len() int

@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

func (v *Vector) Pop() (any, error)

@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

func (v *Vector) Push(item any) Vector

@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] ```

func (*Vector) RemoveAt

func (v *Vector) RemoveAt(index int) error

@api(Container/Vector.RemoveAt) removes the item at the specified index.

func (Vector) Set

func (v Vector) Set(index int, item any) error

@api(Container/Vector.Set) sets the item at the specified index. It returns an error if the index is out of range.

func (*Vector) Splice

func (v *Vector) Splice(start, count int, items ...any) error

@api(Container/Vector.Splice) removes count items starting from the specified index and inserts new items.

Jump to

Keyboard shortcuts

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