Ish*_*han 3 template-engine global-variables partials go-templates hugo
我不熟悉使用Hugo和Go模板.如何使用Hugo从基本文件中定义的部分文件中访问变量?
例如:我有一个index.html文件,其中包含读取存储在events.json数据目录中的文件中的数据并将其存储在变量中的代码.如何从其他文件访问该变量?
的index.html
{{ $events := .Site.Data.events }}
{{ partial "people" . }}
Run Code Online (Sandbox Code Playgroud)
people.html
// access the events variable from the index.html
{{ $events }}
Run Code Online (Sandbox Code Playgroud)
我真的希望这是有道理的.如果需要,我可以尝试澄清更多.
的index.html
{{ $events := .Site.Data.events }}
{{ partial "people" (dict "events" $events) }}
Run Code Online (Sandbox Code Playgroud)
people.html
// access the events variable from the index.html
{{ .events }}
Run Code Online (Sandbox Code Playgroud)
你可以使用dict函数:
{{ partial "people" (dict "page" . "events" $events) }}
然后,将解决他们像{{ .page.someVar }}和{{ .events.someVar }}在部分。
在您的情况下,另一种选择可能是在部分中(如前一张海报所说),.Site.Data.events直接从部分中解决。