gruntwork-io / boilerplate

A tool for generating files and folders ("boilerplate") from a set of templates
https://www.gruntwork.io
Mozilla Public License 2.0
160 stars 12 forks source link

Basic string interpolation fails in range block. #24

Closed josh-padnick closed 7 years ago

josh-padnick commented 7 years ago

Given:

boilerplate.yml:

- name: Route53PublicHostedZones
   description: Enter the domain names (e.g. example.com) for which we should create a Route53 Public Hosted Zone
   type: list
   default:
     - bolste.com
     - bolste.red

- name: HostedZoneComment
    description: Enter the comment field for the Hosted Zone
    type: string
    default: Managed by Terraform

main.tf:

{{- range $index, $domainName := .Route53PublicHostedZones }}

resource "aws_route53_zone" "{{ $domainName | snakeCase }}" {
  name = "{{ $domainName }}"
  comment = "{{ .HostedZoneComment }}"
}
{{- end }}

I would expect this to work fine, but in fact, it fails with:

[boilerplate] 2016/12/22 14:36:42 Processing template ../../../patterns/infrastructure-live/global/route53/main.tf and writing to /repos/gruntwork-clients/bolste/infrastructure-live/global/route53/main.tf
template: ../../../patterns/infrastructure-live/global/route53/main.tf:25:15: executing "../../../patterns/infrastructure-live/global/route53/main.tf" at <.HostedZoneComment>: can't evaluate field HostedZoneComment in type string

Any thoughts on what's going on here?

brikis98 commented 7 years ago

I'm not an expert on Go template syntax, but I believe the dot syntax ({{ .foo }}) means "look up foo in the current context". When you're in a range loop, that context is the items in the loop, of which HostedZoneComment is not one. To get out of that context, you probably need to do something like {{ $.HostedZoneComment }}. http://stackoverflow.com/questions/16734503/access-out-of-loop-value-inside-golang-templates-loop

josh-padnick commented 7 years ago

That did it! Thx for the help. Guess I should have tried more Googling on that one.