【发布时间】:2013-02-10 17:03:47
【问题描述】:
当在with 或range 中时,. 的范围会发生变化。如何访问调用范围?
【问题讨论】:
标签: go go-templates
当在with 或range 中时,. 的范围会发生变化。如何访问调用范围?
【问题讨论】:
标签: go go-templates
{{with .Inner}}
Outer: {{$.OuterValue}}
Inner: {{.InnerValue}}
{{end}}
$ 记录在 text/template 文档中:
开始执行时,$ 设置为传递给 Execute 的数据参数,即设置为 dot 的起始值。
【讨论】:
您可以使用变量保存调用范围:
{{ $save := . }}
{{ with .Inner }}
Outer: {{ $save.OuterValue }}
Inner: {{ .InnerValue }}
{{ end }}
【讨论】: