When inside a with
or range
, the scope of .
is changed. How do you access the calling scope?
Testuser
{{with .Inner}}
Outer: {{$.OuterValue}}
Inner: {{.InnerValue}}
{{end}}
$
is documented in the text/template docs:
When execution begins, $ is set to the data argument passed to Execute, that is, to the starting value of dot.
You can save the calling scope with a variable:
{{ $save := . }}
{{ with .Inner }}
Outer: {{ $save.OuterValue }}
Inner: {{ .InnerValue }}
{{ end }}
来源:https://stackoverflow.com/questions/14800204/in-a-template-how-do-you-access-an-outer-scope-while-inside-of-a-with-or-rang