Skip to content

Helper Functions

Boilerplate provides a rich set of helper functions available in all template files. Helper functions transform values using the pipe (|) operator.

Given a variable ProjectName with the value "My Cool App":

{{ .ProjectName | lower }} → my cool app
{{ .ProjectName | snakecase }} → my_cool_app
{{ .ProjectName | kebabcase }} → my-cool-app
{{ .ProjectName | dasherize }} → my-cool-app
{{ .ProjectName | camelcase }} → MyCoolApp
{{ .ProjectName | camelCaseLower }} → myCoolApp

You can also chain multiple helpers together:

{{ .ProjectName | lower | replace " " "-" }} → my-cool-app
FunctionDescriptionExample
dasherizeConvert to dash-separated lowercase{{ "Foo Bar" | dasherize }}foo-bar
camelCaseLowerLower camelCase{{ "foo-bar" | camelCaseLower }}fooBar
replaceOneReplace first occurrence{{ "aab" | replaceOne "a" "b" }}bab

All Sprig v3 string functions are available:

FunctionDescription
lowerLowercase
upperUppercase
titleTitle Case
trimRemove leading/trailing whitespace
trimPrefixRemove prefix
trimSuffixRemove suffix
replaceReplace all occurrences
containsCheck if string contains substring
hasPrefixCheck if string starts with prefix
hasSuffixCheck if string ends with suffix
snakecaseConvert to snake_case
camelcaseConvert to CamelCase
kebabcaseConvert to kebab-case
quoteWrap in double quotes
indentIndent each line
nindentIndent with leading newline
FunctionDescriptionExample
plusAddition{{ plus 2 3 }}5
minusSubtraction{{ minus 10 3 }}7
timesMultiplication{{ times 4 5 }}20
divideDivision{{ divide 10 3 }}3.333...
roundIntRound to integer{{ roundInt 3.7 }}4
ceilIntCeiling integer{{ ceilInt 3.2 }}4
floorIntFloor integer{{ floorInt 3.8 }}3
numRangeGenerate number list{{ numRange 1 5 1 }}[1 2 3 4]
FunctionDescription
keysSortedReturn sorted keys of a map
toYamlConvert value to YAML string
fromYamlParse YAML string into a value
FunctionDescription
snippetExtract file contents, optionally by named marker
includeInclude and render another template file
pathExistsCheck if a file path exists
shellExecute a shell command and capture output

Include the contents of a file, optionally extracting a named section:

{{ snippet "../src/handler.go" }}

With named sections (delimited by boilerplate-snippet: <name> comments in the source file):

{{ snippet "../src/handler.go" "main-handler" }}

Include and render another template file with the current variables:

{{ include "../shared/header.html" . }}

Run a shell command and insert its output. This is useful for dynamically fetching values at render time — for example, looking up an AWS account ID, reading a secret, or grabbing the current git commit — and interpolating the result directly into your generated files.

Git SHA: {{ shell "git" "rev-parse" "HEAD" }}

More examples:

AWS Account ID: {{ shell "aws" "sts" "get-caller-identity" "--query" "Account" "--output" "text" }}
Current date: {{ shell "date" "+%Y-%m-%d" }}
Resolved IP: {{ shell "dig" "+short" "example.com" }}
FunctionDescription
templateFolderAbsolute path to the template directory
templateUrlThe template URL as provided
outputFolderAbsolute path to the output directory
varsReturn all variables as a map
templateIsDefinedCheck if a named partial/template is defined
envWithDefaultGet an environment variable with a fallback value
relPathCreate a relative path from two absolute paths
Template is at: {{ templateFolder }}
Output goes to: {{ outputFolder }}
Git branch: {{ envWithDefault "BRANCH" "main" }}

These older function names still work for backwards compatibility but should be replaced with their modern equivalents:

DeprecatedUse Instead
downcaselower (Sprig)
upcaseupper (Sprig)
capitalizetitle (Sprig)
snakeCasesnakecase (Sprig)
camelCasecamelcase (Sprig)
roundroundInt
ceilceilInt
floorfloorInt
envenvWithDefault
keyskeysSorted
replacereplaceOne or Sprig’s replace
sliceSprig’s list