ztplzgo.at/ztpl/tplfunc Index | Files

package tplfunc

import "zgo.at/ztpl/tplfunc"

Index

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,

	"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

func Abs(n any) float64

Abs gets the absolute value of n.

func Add

func Add(name string, f any)

Add a new template function.

func Cat

func Cat(args ...any) string

Cat/join any arguments.

func Checkbox

func Checkbox(current any, name string) template.HTML

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

func Checked(vals []int64, id int64) template.HTMLAttr

Checked returns a 'checked="checked"' attribute if id is in vals.

func Contains

func Contains(slice any, find any) bool

Contains reports if the slice contains the element value find.

func Deref

func Deref(s any) any

Deref dereferences a pointer.

func Div

func Div(n, n2 any, n3 ...any) any

Div all the given numbers.

func Duration

func Duration(a any, format string) string

func Elide

func Elide(s string, n int) string

Elide a string to at most n characters.

func HasPrefix

func HasPrefix(s, prefix string) bool

HasPrefix tests whether the string s begins with prefix.

func HasSuffix

func HasSuffix(s, suffix string) bool

HasSuffix tests whether the string s ends with suffix.

func If2

func If2(cond bool, yes any, no ...any) any

If2 returns yes if cond is true, and no otherwise.

func Int

func Int(n any) int64

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

func IsInf(n float64) bool

IsInf reports if n is Inf.

func JSON

func JSON(v any) string

JSON prints any object as JSON.

func JSONPretty

func JSONPretty(v any) string

JSONPretty prints any object as indented JSON.

func LargeNumber

func LargeNumber(n any, sep ...rune) string

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

func Map(values ...any) map[string]any

Map creates a map

func Max

func Max(a, b any) float64

func Min

func Min(a, b any) float64

func Mult

func Mult(n, n2 any, n3 ...any) any

Mult multiplies all the given numbers.

func Number

func Number(n any, sep ...rune) string

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

func OptionValue(current, value string) template.HTMLAttr

OptionValue inserts the value attribute, and selected attribute if the value is the same as current.

func Round

func Round(n any, to int) float64

Round n; if to is 0 it will round to nearest, <1 to floor, >1 to ceil.

func Size

func Size(n any, format ...string) string

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 Slug(s string) string

func String

func String(v any) string

String converts anything to a string.

func Sub

func Sub(n, n2 any, n3 ...any) any

Sub subtracts all the given numbers.

func Substr

func Substr(s string, i, j int) string

Substr returns part of a string.

func Sum

func Sum(n, n2 any, n3 ...any) any

Sum all the given numbers.

func Time

func Time(a any, format string) string

Time formats a time as the given format string.

Return empty string if the time is nil or the zero value.

func UCFirst

func UCFirst(s string) string

UCFirst converts the first letter to uppercase, and the rest to lowercase.

func Unsafe

func Unsafe(s any) template.HTML

Unsafe converts a string to template.HTML, preventing any escaping.

Can be dangerous if used on untrusted input!

func UnsafeJS

func UnsafeJS(s string) template.JS

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-20231210165448-05891fb9a4df (latest)
Published
Dec 10, 2023
Platform
linux/amd64
Imports
10 packages
Last checked
1 month ago

Tools for package owners.