tempfunc

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2021 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package tempfunc provide template functions. The provided functions are of two kinds : for random generation, and for general (numerical) manipulation.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func RandomFunctions

func RandomFunctions(seed int64) template.FuncMap

RandomFunctions provides template functions for random generation/selection.

Example (Pick)

The function `pick` select one of the provided parameters.

const hello string = `Hello, {{ pick "apple" "banana" 7 }}!`
// the random generator use 42 as seed
rf := RandomFunctions(42)
// compile and execute the template (without error check, very bad idea!)
t, _ := template.New("hi").Funcs(rf).Parse(hello)
t.Execute(os.Stdout, nil)
Output:

Hello, 7!
Example (Rand)

The function `randf` provides a random float between the two parameters (excluding the second one). The function `randi` provides a random integer between (including) the two parameters.

// the parameters of randf are loosely numeric
const hello string = `Hello, {{ randf "1" 3 }} and {{ randi "1" 3 }} !`
// the random generator use 42 as seed
rf := RandomFunctions(42)
// compile and execute the template (without error check, very bad idea!)
t, _ := template.New("hi").Funcs(rf).Parse(hello)
t.Execute(os.Stdout, nil)
Output:

Hello, 1.7460567220932652 and 3 !

func UtilFunctions

func UtilFunctions() template.FuncMap

UtilFunctions provides a set of utility template functions.

Example (Fromto)

The parameters of `fromto` are rounded. The generated sequence include the second parameter.

const hello string = `{{ $r := fromto "1.1" -2 }} Hello{{ range $r }}, {{.}}{{ end }} !`
// compile and execute the template (without error check, very bad idea!)
t, _ := template.New("hi").Funcs(UtilFunctions()).Parse(hello)
t.Execute(os.Stdout, nil)
Output:

Hello, 1, 0, -1, -2 !
Example (List)
const hello string = `Hello{{ range (list 1 2 4) }}, {{ . | times 2 }}{{ end }} !`
// compile and execute the template (without error check, very bad idea!)
t, _ := template.New("hi").Funcs(UtilFunctions()).Parse(hello)
t.Execute(os.Stdout, nil)
Output:

Hello, 2, 4, 8 !
Example (Number)

The functions `number` and `float` are the same.

const hello string = `Hello, {{ "1.2e3" | number }} and {{ "not a number" | number }} !`
// compile and execute the template (without error check, very bad idea!)
t, _ := template.New("hi").Funcs(UtilFunctions()).Parse(hello)
t.Execute(os.Stdout, nil)
Output:

Hello, 1200 and 0 !
Example (Round)
const hello string = `Hello, {{ "1.7e-3" | round "3" }} and {{ 1.00001 | round 1 }} !`
// compile and execute the template (without error check, very bad idea!)
t, _ := template.New("hi").Funcs(UtilFunctions()).Parse(hello)
t.Execute(os.Stdout, nil)
Output:

Hello, 0.002 and 1 !
Example (Times)

The function `plus` can be used in the same way as `times`.

const hello string = `Hello, {{ "1.2e3" | times .01 }} and {{ times -1 "3" }} !`
// compile and execute the template (without error check, very bad idea!)
t, _ := template.New("hi").Funcs(UtilFunctions()).Parse(hello)
t.Execute(os.Stdout, nil)
Output:

Hello, 12 and -3 !
Example (Upto)

The `upto` starts from 0 and do not include the (rounded) parameter value. If the parameter is 0 or less, an empty list is provided.

const hello string = `{{ $r := upto 3 }} Hello{{ range $r }}, {{.}}{{ end }} !`
// compile and execute the template (without error check, very bad idea!)
t, _ := template.New("hi").Funcs(UtilFunctions()).Parse(hello)
t.Execute(os.Stdout, nil)
Output:

Hello, 0, 1, 2 !
Example (Varset)
const hello string = `{{ $what := var "initial" -}}
	{{- if eq ("1.001" | round 1) "1" }}
		{{- "from inside" | set $what }}
	{{- end }}
	Hello {{ $what }} !`
// compile and execute the template (without error check, very bad idea!)
t, _ := template.New("hi").Funcs(UtilFunctions()).Parse(hello)
t.Execute(os.Stdout, nil)
Output:

Hello from inside !

Types

This section is empty.

Jump to

Keyboard shortcuts

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