我在 Hugo 的一个部分中使用了这段代码来将上下文传递给它。
{{- $ctx := . -}}
{{- $curPage := .page -}}
{{- $otherVar := .otherVar -}}
{{- with $curPage -}}
{{ $section := .CurrentSection }}
{{ if .IsHome }}
<span class="post-section"><a href="{{ $section.Permalink }}" target="_blank">{{ $section.Title }}</a></span>
{{ else }}
<a href="{{ $section.Permalink }}"> {{ $section.Title }}</a>
{{ end }}
{{- end -}}
{{- $curPage := . -}}
然后我在模板顶部添加我希望部分显示的位置,然后将部分调用为{{ partial "partial-name.html" (dict "page" $curPage "otherVar" .) }}
. 但是,该内容在主页上返回 nil,而它在站点范围内的其他任何地方都可以使用。谁能看看我的代码并告诉我哪里出错了?