package tplfunc
import "zgo.at/ztpl/tplfunc"
Index ¶
- Variables
- func Abs(n any) float64
- func Add(name string, f any)
- func After(t time.Time, tt string) bool
- func Before(t time.Time, tt string) bool
- func Cat(args ...any) string
- func Checkbox(current any, name string) template.HTML
- func Checked(vals []int64, id int64) template.HTMLAttr
- func Contains(slice any, find any) bool
- func Deref(s any) any
- func Div(n, n2 any, n3 ...any) any
- func Duration(a any, format string) string
- func Elide(s string, n int) string
- func HasPrefix(s, prefix string) bool
- func HasSuffix(s, suffix string) bool
- func If2(cond bool, yes any, no ...any) any
- func Int(n any) int64
- func IsInf(n float64) bool
- func JSON(v any) string
- func JSONPretty(v any) string
- func LargeNumber(n any, sep ...rune) string
- func Map(values ...any) map[string]any
- func Max(a, b any) float64
- func Min(a, b any) float64
- func Mult(n, n2 any, n3 ...any) any
- func Number(n any, sep ...rune) string
- func OptionValue(current, value string) template.HTMLAttr
- func Round(n any, to int) float64
- func Size(n any, format ...string) string
- func Slug(s string) string
- func String(v any) string
- func Sub(n, n2 any, n3 ...any) any
- func Substr(s string, i, j int) string
- func Sum(n, n2 any, n3 ...any) any
- func Time(a any, format string) string
- func UCFirst(s string) string
- func Unsafe(s any) template.HTML
- func UnsafeJS(s string) template.JS
Variables ¶
var FuncMap = template.FuncMap{ "int": Int, "sum": Sum, "sub": Sub, "mult": Mult, "div": Div, "round": Round, "abs": Abs, "is_inf": IsInf, "min": Min, "max": Max, "substr": Substr, "elide": Elide, "has_prefix": HasPrefix, "has_suffix": HasSuffix, "join": strings.Join, "ucfirst": UCFirst, "cat": Cat, "deref": Deref, "if2": If2, "map": Map, "contains": Contains, "before": Before, "after": After, "json": JSON, "json_pretty": JSONPretty, "number": Number, "large_number": LargeNumber, "time": Time, "duration": Duration, "size": Size, "slug": Slug, }
FuncMap contains all the template functions.
var FuncMapHTML = map[string]any{ "unsafe": Unsafe, "unsafe_js": UnsafeJS, "checkbox": Checkbox, "checked": Checked, "option_value": OptionValue, }
Functions ¶
func Abs ¶
Abs gets the absolute value of n.
func Add ¶
Add a new template function.
func After ¶
t is after tt.
func Before ¶
t is before tt.
func Cat ¶
Cat/join any arguments.
func Checkbox ¶
Checkbox adds a checkbox; if current is true then it's checked.
It also adds a hidden input with the value "off" so that's sent to the server when the checkbox isn't sent, which greatly simplifies backend handling.
func Checked ¶
Checked returns a 'checked="checked"' attribute if id is in vals.
func Contains ¶
Contains reports if the slice contains the element value find.
func Deref ¶
Deref dereferences a pointer.
func Div ¶
Div all the given numbers.
func Duration ¶
func Elide ¶
Elide a string to at most n characters.
func HasPrefix ¶
HasPrefix tests whether the string s begins with prefix.
func HasSuffix ¶
HasSuffix tests whether the string s ends with suffix.
func If2 ¶
If2 returns yes if cond is true, and no otherwise.
func Int ¶
Int converts any int, float, or string to an integer.
Floats are always rounded down; strings and []byte is parsed as a base-10 number. Any other type will panic.
func IsInf ¶
IsInf reports if n is Inf.
func JSON ¶
JSON prints any object as JSON.
func JSONPretty ¶
JSONPretty prints any object as indented JSON.
func LargeNumber ¶
LargeNumber formats a number, adding the suffix "M" for values larger than a million or "k" for values larger than 100,000.
This loses some accuracy.
func Map ¶
Map creates a map
func Max ¶
func Min ¶
func Mult ¶
Mult multiplies all the given numbers.
func Number ¶
Number formats a number with thousand separators using the separator sep.
For floats it will always use '.' as the digit separator, unless sep is set to '.' in which case it will use ','.
func OptionValue ¶
OptionValue inserts the value attribute, and selected attribute if the value is the same as current.
func Round ¶
Round n; if to is 0 it will round to nearest, <1 to floor, >1 to ceil.
func Size ¶
Size formats a file size.
The optional parameter max gives the highest unit to format it as. Values for this can be 'b', 'k', 'm', 'g, 't', 'p'.
The format string controls some formatting aspects, as key/value pairs:
min=n max=n from=n
func Slug ¶
func String ¶
String converts anything to a string.
func Sub ¶
Sub subtracts all the given numbers.
func Substr ¶
Substr returns part of a string.
func Sum ¶
Sum all the given numbers.
func Time ¶
Time formats a time as the given format string.
Return empty string if the time is nil or the zero value.
func UCFirst ¶
UCFirst converts the first letter to uppercase, and the rest to lowercase.
func Unsafe ¶
Unsafe converts a string to template.HTML, preventing any escaping.
Can be dangerous if used on untrusted input!
func UnsafeJS ¶
UnsafeJS converts a string to template.JS, preventing any escaping.
Can be dangerous if used on untrusted input!
Source Files ¶
fmt.go html.go math.go string.go tplfunc.go
- Version
- v0.0.0-20240522225602-574aca1079e7 (latest)
- Published
- May 22, 2024
- Platform
- linux/amd64
- Imports
- 11 packages
- Last checked
- 3 days ago –
Tools for package owners.