Templates

Templates

Many filters in driplane support Go templates in their parameters. Templates allow you to dynamically compose strings using the fields of the current Message (both the main field and any extra fields).

Templates are based on the standard Go text/template package and are extended with all the functions from the Sprig library, giving you access to over 100 useful functions for string manipulation, math, date formatting, encoding, and more.

Basic syntax

Inside a template, you can reference any field of the Message using the {{ .field_name }} syntax:

  • {{ .main }} - the main text of the Message
  • {{ .file_name }} - an extra field called file_name
  • {{ .user_username }} - an extra field called user_username
... | format(template="Author: {{ .author }} wrote: {{ .main }}") | ...

Sprig functions

All Sprig functions are available in templates. Functions can be called using the pipe | operator or the function call syntax.

String functions

FunctionDescriptionExampleResult
upperConvert to uppercase{{ .main | upper }}HELLO
lowerConvert to lowercase{{ .main | lower }}hello
trimRemove leading/trailing whitespace{{ .main | trim }}hello
trimPrefixRemove prefix{{ .main | trimPrefix "http://" }}example.com
trimSuffixRemove suffix{{ .main | trimSuffix ".txt" }}file
replaceReplace occurrences{{ .main | replace " " "_" }}hello_world
containsCheck if string contains substring{{ if contains "error" .main }}...{{ end }}
hasPrefixCheck prefix{{ if hasPrefix "http" .main }}...{{ end }}
hasSuffixCheck suffix{{ if hasSuffix ".pdf" .main }}...{{ end }}
repeatRepeat a string N times{{ .main | repeat 3 }}hellohellohello
substrExtract substring{{ substr 0 5 .main }}hello
nospaceRemove all whitespace{{ .main | nospace }}helloworld
truncTruncate to length{{ .main | trunc 5 }}hello
abbrevAbbreviate with ellipsis{{ .main | abbrev 10 }}hello w...
quoteWrap in double quotes{{ .main | quote }}"hello"
squoteWrap in single quotes{{ .main | squote }}'hello'
titleConvert to title case{{ .main | title }}Hello World
snakecaseConvert to snake_case{{ .main | snakecase }}hello_world
camelcaseConvert to camelCase{{ .main | camelcase }}HelloWorld
kebabcaseConvert to kebab-case{{ .main | kebabcase }}hello-world

Encoding functions

FunctionDescriptionExample
b64encBase64 encode{{ .main | b64enc }}
b64decBase64 decode{{ .main | b64dec }}
sha256sumSHA256 hash{{ .main | sha256sum }}
sha1sumSHA1 hash{{ .main | sha1sum }}

Default and conditional functions

FunctionDescriptionExample
defaultProvide a default value{{ .name | default "unknown" }}
emptyCheck if value is empty{{ if empty .main }}no content{{ end }}
coalesceReturn first non-empty value{{ coalesce .name .username "anonymous" }}
ternaryTernary operator{{ ternary "yes" "no" (contains "ok" .main) }}

List functions

FunctionDescriptionExample
listCreate a list{{ list "a" "b" "c" }}
joinJoin list elements{{ list "a" "b" "c" | join "," }}
splitSplit string into map{{ (split "," .main)._0 }}
splitListSplit string into list{{ .main | splitList "," }}

Math functions

FunctionDescriptionExample
addAddition{{ add 1 2 }}
subSubtraction{{ sub 10 3 }}
mulMultiplication{{ mul 2 5 }}
divDivision{{ div 10 2 }}
maxMaximum value{{ max 1 5 3 }}
minMinimum value{{ min 1 5 3 }}

Date functions

FunctionDescriptionExample
nowCurrent time{{ now }}
dateFormat date{{ now | date "2006-01-02" }}
dateModifyModify a date{{ now | dateModify "-24h" }}

Chaining functions

Functions can be chained using the pipe operator:

{{ .main | lower | trim | replace " " "_" }} - lowercase, trim whitespace, replace spaces with underscores

{{ .main | upper | trunc 50 }} - uppercase then truncate to 50 characters

{{ .name | default "anonymous" | upper }} - use default if empty, then uppercase

Conditionals

Go templates support conditional logic:

{{ if contains "error" .main }}ALERT: {{ .main }}{{ else }}{{ .main }}{{ end }}

{{ if and (not (empty .name)) (contains "@" .name) }}{{ .name }}{{ end }}

Filters supporting templates

The following filters accept Go templates (with Sprig functions) in one or more of their parameters:

FilterParameters supporting templates
Formattemplate, file
Overridename, value
Systemcmd
Httpurl, download_to, data values, rawData
Mailbody
Slackto, text, filename, url
Telegramto, to_chatid, filename, text
LLMprompt, system_prompt
Pdffilename
XLSfilename
MIME typefilename

More information

For the complete list of Sprig functions, visit the Sprig documentation.

For the full Go template syntax, see the Go text/template documentation.