Documentation › JSON API › Template functions
If you select the emails options when generating your codebase, it will contain an internal/funcs/funcs.go file with a selection of helper functions for use in email templates. These functions are automatically available in your email templates when using app.mailer.Send().
The following functions are included:
| Function | Description | Example |
|---|---|---|
now |
Returns the current time as time.Time. |
{{ now }} |
timeSince arg1 |
Returns the time elapsed since arg1 time.Time as a time.Duration. |
{{ timeSince .CreatedAt }} |
timeUntil arg1 |
Returns the time until arg1 time.Time as a time.Duration. |
{{ timeUntil .ExpiresAt }} |
formatTime arg1 arg2 |
Returns arg2 time.Time formatted using the pattern arg1 string. |
{{ formatTime "2006-01-02" .CreatedAt }} |
approxDuration arg1 |
Returns the approximate duration of arg1 time.Duration in a human-friendly format. |
{{ approxDuration (timeSince .CreatedAt) }} |
uppercase arg1 |
Returns arg1 string converted to uppercase. |
{{ uppercase .Name }} |
lowercase arg1 |
Returns arg1 string converted to lowercase. |
{{ lowercase .Name }} |
pluralize arg1 arg2 arg3 |
If arg1 any equals 1, returns arg2 string; otherwise returns arg3 string. |
{{ pluralize .Count "item" "items" }} |
slugify arg1 |
Returns arg1 string in lowercase with non-ASCII characters and punctuation removed, and whitespace replaced with hyphens. |
{{ slugify .Title }} |
safeHTML arg1 |
Outputs arg1 string as template.HTML without escaping. Use only with trusted input. |
{{ safeHTML .Content }} |
join arg1 arg2 |
Returns the values in slice arg1 []string joined using the separator arg2 string. |
{{ join .Tags ", " }} |
incr arg1 |
Increments arg1 any by 1 and returns an int64. |
{{ incr .Index }} |
decr arg1 |
Decrements arg1 any by 1 and returns an int64. |
{{ decr .Index }} |
formatInt arg1 |
Returns arg1 any formatted with commas as the thousands separator. |
{{ formatInt .Count }} |
formatFloat arg1 arg2 |
Returns arg1 float64 rounded to arg2 int decimal places and formatted with commas. |
{{ formatFloat .Price 2 }} |
yesNo arg1 |
Returns "Yes" if arg1 bool is true, or "No" if false. |
{{ yesNo .Active }} |
urlSetParam arg1 arg2 arg3 |
Returns arg1 *url.URL with the key arg2 string and value arg3 any added to the query string. |
{{ urlSetParam .URL "page" 2 }} |
urlDelParam arg1 arg2 |
Returns arg1 *url.URL with the key arg2 string removed from the query string. |
{{ urlDelParam .URL "page" }} |
Feel free to edit the internal/funcs/funcs.go file to include any additional functions you need in your templates.
To add a function, define it in internal/funcs/funcs.go and register it in the TemplateFuncs map. For example:
var TemplateFuncs = template.FuncMap{
...
"yourFunction": yourFunction,
}
func yourFunction(s string) (string, error) {
// Do something...
}
Note that template functions must return either a single value, or two values where the second has type error. See the documentation for template.FuncMap for more details.