Documentation ¶
Overview ¶
Package funcmap provides functions useful to html templates, and a standard html.FuncMap of them.
Functions Defined Here ¶
These correspond to the exported function names. By convention(?) the map keys, i.e. the function names called in the template, are lowercased.
truncints truncintsto sortints sortintsasc sortintsdesc reverseints intrange pathdepth indent
Functions From Kyoung-chan Lee's Gtf ¶
See https://github.com/leekchan/gtf for documentation.
replace default length lower upper truncatechars urlencode wordcount divisibleby lengthis trim capfirst pluralize yesno rjust ljust center filesizeformat apnumber intcomma ordinal first last join slice random striptags
Example ¶
package main import ( "github.com/biztos/kisipar/funcmap" "html/template" "os" ) func main() { dot := map[string]interface{}{ "Title": "Hello World!", "Id": "examples", "Prices": []int{ 15, 11800002, 3582, 999231, 10012, }, } var tsrc = `<h1 id="{{ .Id | capfirst }}">{{ .Title }}</h1> <h2>Top Three Prices:</h2> <ol> {{- $p := sortintsdesc .Prices | truncintsto 3 }}{{ range $p }} <li>${{ intcomma . }}</li> {{- end }} </ol> ` tmpl, err := template.New("test").Funcs(funcmap.New()).Parse(tsrc) if err != nil { panic(err) } if err := tmpl.Execute(os.Stdout, dot); err != nil { panic(err) } }
Output: <h1 id="Examples">Hello World!</h1> <h2>Top Three Prices:</h2> <ol> <li>$11,800,002</li> <li>$999,231</li> <li>$10,012</li> </ol>
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func New ¶
New returns a function map containing all the available functions, mapped to names as shown in the documentation above.
func PathDepth ¶
PathDepth returns the depth of a cleaned URL path (or similar string), i.e. the number of slashes it contains.
func ReverseInts ¶
ReverseInts returns a copy of the input slice in reverse order, i.e. the opposite of the given order.
func SortIntsDesc ¶
SortIntsDesc returns a reverse-sorted (Descending) copy of a slice of integers.
func TruncIntsTo ¶
TruncIntsTo returns a slice of the input slice with a maximum of t elements. If t is negative, truncates to len(i) - t. Note the argument order: this is to facilitate piping in the template:
{{ $r := .SomeIntSlice | truncintsto 5 }}
Types ¶
This section is empty.