Skip to content

Commit

Permalink
docs: add templatestring
Browse files Browse the repository at this point in the history
  • Loading branch information
kmoe committed May 24, 2024
1 parent 9563ed7 commit f25a95d
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
9 changes: 9 additions & 0 deletions website/data/language-nav-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,10 @@
"title": "<code>substr</code>",
"href": "/language/functions/substr"
},
{
"title": "<code>templatestring</code>",
"href": "/language/functions/templatestring"
},
{
"title": "<code>title</code>",
"href": "/language/functions/title"
Expand Down Expand Up @@ -893,6 +897,11 @@
"path": "functions/templatefile",
"hidden": true
},
{
"title": "templatestring",
"path": "functions/templatestring",
"hidden": true
},
{ "title": "terraform-encode_tfvars", "path": "functions/terraform-encode_tfvars", "hidden": true },
{ "title": "terraform-decode_tfvars", "path": "functions/terraform-decode_tfvars", "hidden": true },
{ "title": "terraform-encode_expr", "path": "functions/terraform-encode_expr", "hidden": true },
Expand Down
1 change: 1 addition & 0 deletions website/docs/language/functions/templatefile.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,4 @@ For more information, see the main documentation for

* [`file`](/terraform/language/functions/file) reads a file from disk and returns its literal contents
without any template interpretation.
* [`templatestring`](/terraform/language/functions/templatestring) takes a simple reference to a string value containing the template and renders its content.
68 changes: 68 additions & 0 deletions website/docs/language/functions/templatestring.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
page_title: templatestring - Functions - Configuration Language
description: |-
The templatestring function takes a string from elsewhere in the module and renders its content as a template using a supplied set of template variables.
---

# `templatestring` Function

-> **Note:** The `templatestring` function is intended for advanced use cases, and is not the same as a [string template expression](/terraform/language/expressions/strings#string-templates). To render a template from a file, use the [`templatefile` function](/terraform/language/functions/templatefile).

`templatestring` reads the file at the given path and renders its content
as a template using a supplied set of template variables.

```hcl
templatefile(ref, vars)
```

The first parameter must be a simple reference to string value containing the template: for example, `data.aws_s3_object.example.body` or `local.inline_template`.

It is ***not** valid to supply the template expression directly as the first argument:

```hcl
# The following is not allowed
templatestring("Hello, $${name}", {
name = var.name
})
```

Instead of the above, you should instead use a string template expression:

```hcl
"Hello, ${var.name}"
```

The `templatestring` function is needed only when the template is available as a named object in the current module.

The template syntax is the same as for
[string templates](/terraform/language/expressions/strings#string-templates)
in the main Terraform language, including interpolation sequences delimited with
`${` ... `}`.

Strings in the Terraform language are sequences of Unicode characters, so
this function will interpret the file contents as UTF-8 encoded text and
return the resulting Unicode characters. If the template contains invalid UTF-8
sequences then this function will produce an error.

## Example

The following example retrieves a template from S3 and dynamically renders it:

```hcl
data "aws_s3_object" "example" {
bucket = "example-example"
key = "example.tmpl"
}
output "example" {
value = templatestring(data.aws_s3_object.example.body, {
name = var.name
})
}
```

For more examples of how to use templates, please see the documentation for the [`templatefile`](/terraform/language/functions/templatefile#Examples) function.

## Related Functions

* [`templatefile`](/terraform/language/functions/templatefile) reads a file from disk and renders its content as a template.

0 comments on commit f25a95d

Please sign in to comment.