Documentation
¶
Overview ¶
Package tmplfuncs provides small, reusable helper functions for Go templates.
The helpers are designed for use with text/template and html/template. They are pipeline-friendly where that makes templates easier to read.
Index ¶
- func CoalesceValue(values ...any) any
- func DefaultValue(fallback, value any) any
- func DurationValue(value any) (string, error)
- func FormatTimeValue(layout string, value any) (string, error)
- func FuncMap(helpers ...Helper) template.FuncMap
- func FuncMapE(helpers ...Helper) (template.FuncMap, error)
- func JSONValue(value any) (string, error)
- func LowerValue(value any) string
- func OptionalValue(format string, values ...any) string
- func TrimValue(value any) string
- func UpperValue(value any) string
- func WhenValue(condition, trueValue, falseValue any) any
- func WithPrefixValue(prefix string, value any) string
- func WithSuffixValue(suffix string, value any) string
- type Helper
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CoalesceValue ¶
CoalesceValue returns the first non-empty value.
Empty follows the same rules as DefaultValue.
{{ coalesce .Title .Name "unknown" }}
func DefaultValue ¶
DefaultValue returns fallback when value is empty.
Empty follows the same rules as template helpers: nil, false, zero numbers, empty strings, empty arrays, empty maps, empty slices, empty channels, nil pointers/interfaces, and other Go zero values are empty.
The argument order supports both direct and pipeline usage:
{{ default "fallback" .Value }}
{{ .Value | default "fallback" }}
func DurationValue ¶
DurationValue renders a duration value as a Go duration string.
The value may be time.Duration, *time.Duration, or a string accepted by time.ParseDuration.
{{ .AlertingDelay | duration }}
func FormatTimeValue ¶
FormatTimeValue formats a time value with layout.
The value may be time.Time, *time.Time, or an RFC3339/RFC3339Nano string. The argument order supports both direct and pipeline usage:
{{ formatTime "2006-01-02 15:04:05 MST" .ExpectedBy }}
{{ .ExpectedBy | formatTime "2006-01-02 15:04:05 MST" }}
func FuncMap ¶
FuncMap returns a template.FuncMap containing the selected helpers.
When called without arguments, FuncMap returns all helpers. When called with helpers, it returns only those helpers.
FuncMap panics for unknown helper identifiers because invalid helper identifiers are programming errors. Use FuncMapE when helper identifiers come from dynamic input.
func FuncMapE ¶ added in v0.0.2
FuncMapE returns a template.FuncMap containing the selected helpers.
When called without arguments, FuncMapE returns all helpers. When called with helpers, it returns only those helpers.
FuncMapE returns an error for unknown helper identifiers.
func JSONValue ¶
JSONValue renders value as a JSON literal.
This is useful when embedding dynamic strings, maps, slices, or structs in JSON templates.
"text": {{ .Subject | json }}
func OptionalValue ¶
OptionalValue formats text only when all values are non-empty.
Empty follows the same rules as DefaultValue. Values are converted to strings and trimmed before formatting, so whitespace-only values are treated as empty.
The argument order supports direct and pipeline usage:
{{ optional "Status URL: %s" .App.StatusURL }}
{{ optional "%s: %s" .Label .Value }}
{{ .App.StatusURL | optional "Status URL: %s" }}
func TrimValue ¶
TrimValue returns value as a string with surrounding whitespace removed.
{{ .Name | trim }}
func WhenValue ¶
WhenValue returns trueValue when condition is truthy, otherwise falseValue.
Truthiness follows template-style rules: nil, false, zero numbers, empty strings, empty arrays, empty maps, empty slices, empty channels, and nil pointers/interfaces are false. Other values are true.
The helper accepts any value type so it can be used with booleans, strings, numbers, slices, maps, and pointers.
{{ when .Resolved "Resolved at" "Notified at" }}
{{ when .Status "has status" "missing status" }}
func WithPrefixValue ¶
WithPrefixValue returns value with prefix prepended when it is not already present.
The value and prefix are trimmed before comparison. The argument order supports both direct and pipeline usage:
{{ withPrefix "#" .CustomData.channel }}
{{ .CustomData.channel | withPrefix "#" }}
func WithSuffixValue ¶
WithSuffixValue returns value with suffix appended when it is not already present.
The value and suffix are trimmed before comparison. The argument order supports both direct and pipeline usage:
{{ withSuffix "/" .Path }}
{{ .Path | withSuffix "/" }}
Types ¶
type Helper ¶
type Helper string
Helper identifies a template helper that can be registered in a FuncMap.
The constant name is exported for Go code. Its string value is the function name registered inside templates.
const ( // JSON registers json, which renders a value as a JSON literal. JSON Helper = "json" // When registers when, which returns one of two values based on template truthiness. When Helper = "when" // Default registers default, which returns a fallback for empty values. Default Helper = "default" // Coalesce registers coalesce, which returns the first non-empty value. Coalesce Helper = "coalesce" // Optional registers optional, which formats text only when all values are non-empty. Optional Helper = "optional" // FormatTime registers formatTime, which formats a time value with a Go layout. FormatTime Helper = "formatTime" // Trim registers trim, which removes surrounding whitespace. Trim Helper = "trim" // Upper registers upper, which converts text to uppercase. Upper Helper = "upper" // Lower registers lower, which converts text to lowercase. Lower Helper = "lower" // WithPrefix registers withPrefix, which prepends a prefix when missing. WithPrefix Helper = "withPrefix" // WithSuffix registers withSuffix, which appends a suffix when missing. WithSuffix Helper = "withSuffix" // Duration registers duration, which renders a time.Duration or duration string. Duration Helper = "duration" )