How to use .Values in other variable loops

Viewed 6696

Below is my case:

{{- $v := (.Files.Get "values-productpage.yaml") | fromYaml }}.   
   spec:
     {{- range $key, $value := $v.containers }}
     containers:
     - name: {{ $value.name }}
       image: {{.Values.productpage_image}}:latest

Here when reaching .Values.productpage_image, it reports: can't evaluate field productpage_image in type interface {}.

Is there any usage error here? Why can I not use .Values.xxx in this loop? If I move the .Values to the first line, there is no error.

3 Answers

As @abinet explained properly about the reason, I'll share my solution for that( which helped me a lot, and I hope that will save you time):

First, I saved the scope:
{{- $root := . -}}

and after that , I called the .Value inside the loop context like this:
{{ $root.Values.data }}

so basically , you code should be look like:

{{- $root := . -}}

{{- $v := (.Files.Get "values-productpage.yaml") | fromYaml }}.   
  spec:
   {{- range $key, $value := $v.containers }}
   containers:
   - name: {{ $value.name }}
     image: {{$root.Values.productpage_image}}:latest
Related