funcmap

package
v0.0.0-...-cf5c4a9 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2023 License: MIT, MIT Imports: 4 Imported by: 0

README

Kisipar funcmap!

TODO

  • Godoc and testing links once public.

What?

A collection of functions useful to have in Go templates.

Why?

Go has an interesting and quite powerful templating system. However, it has very meagre function support out of the box.

This is by design: Go makes it is easy to define your own functions, and thus not waste any time or memory on functions you don't plan to use.

So far so good, until you want a general-purpose templating system for, say, a web site, and you want to give template authors a broad palette of functions.

Hence this collection, which will make use of other collections as well.

How?

TODO

Who?

Kevin Frost, for functions defined in this package directly.

Kyoung-chan Lee, for functions defined in gtf: https://github.com/leekchan/gtf

Whither?

This may grow into some kind of monster collection, but it may also not.

In fact I rather hope it doesn't: other people have more time to spend on this problem, and better reasons to spend it; and I'm more than happy to send them pull requests once I'm convinced my functions have sufficient merit.

In the meantime, funcmap is a subpackage of kisipar, and probably not very interesting outside that context.

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 Indent

func Indent(depth int) string

Indent returns a string with four spaces repeated up to the given depth.

func IntRange

func IntRange(a, b int) []int

IntRange returns an integer slice containing the range from a to b inclusive.

func New

func New() template.FuncMap

New returns a function map containing all the available functions, mapped to names as shown in the documentation above.

func PathDepth

func PathDepth(p string) int

PathDepth returns the depth of a cleaned URL path (or similar string), i.e. the number of slashes it contains.

func ReverseInts

func ReverseInts(ii []int) []int

ReverseInts returns a copy of the input slice in reverse order, i.e. the opposite of the given order.

func SortInts

func SortInts(ii []int) []int

SortInts returns a sorted (Ascending) copy of a slice of integers.

func SortIntsDesc

func SortIntsDesc(ii []int) []int

SortIntsDesc returns a reverse-sorted (Descending) copy of a slice of integers.

func TruncInts

func TruncInts(i []int) []int

TruncInts returns a slice of the input slice with its final element removed.

func TruncIntsTo

func TruncIntsTo(t int, i []int) []int

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.

Jump to

Keyboard shortcuts

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