Templating Functions
Warning
This content is part of the legacy version of Waypoint that is no longer actively maintained. For additional information on the new vision of Waypoint, check out this blog post and the HCP Waypoint documentation.
Waypoint has a set of functions for templating strings, files, and directories with both predefined and custom variable values. These templates can use conditionals, loops, and more to dynamically generate content.
Waypoint has three templating functions documented below: templatefile
,
templatedir
, and templatestring
. All of these functions use the same
templating syntax. The templates have access to all the same variables
and functions that are available at the function call-site (where the
function such as templatedir
is called). Custom variables can also
be provided to the template.
Syntax
Template syntax uses HCL string templates. This supports conditionals, loops, function calls, etc.
templatefile
templatefile
takes a path to a file and optionally one or more maps of variables
for the template, renders the template at path
, and returns the path to
the rendered template. The returned path is a copy of the original template
in a temporary location. This function does not modify the original file.
Reminder: This function returns a file path, not the rendered contents of the file.
If you want the contents of the rendered file, you can wrap this function
with file
.
This function is useful for parameters that expect file paths. The example below shows using this function with Docker to template the Dockerfile:
templatedir
templatedir
is similar to templatefile
, except it takes a path to
a directory, treats all files within that directory as a template, copies
the results to a new temporary directory, and returns the temporary
directory path.
This function will recursively render all files in path
.
This function is useful for parameters that expect directory paths, such as specifying a directory of YAML files for Kubernetes.
templatestring
templatestring
renders a string as a template directly and returns
the rendered contents.
If you have a static template string, you probably want to use
string literals instead. This function is primarily for dynamically
sourced templates (such as reading a file or the contents of some
other variable). If you have a static string you can use HCL interpolations
directly, for example: "Port: ${deploy.port}"
Example, rendering a template file and returning the contents directly: