我刚开始使用 Hugo和去模板。如何使用 Hugo 从基本文件中定义的部分文件中访问变量?
例如:我有一个 index.html
包含读取存储在 events.json
中的数据的代码的文件数据目录中的文件并将其存储在变量中。如何从另一个文件访问该变量?
index.html
{{ $events := .Site.Data.events }}
{{ partial "people" . }}
people.html
// access the events variable from the index.html
{{ $events }}
我真的希望这是有道理的。如果需要,我可以尝试澄清更多。
最佳答案
0.15 引入了 map that can be used for this .
index.html
{{ $events := .Site.Data.events }}
{{ partial "people" (dict "events" $events) }}
people.html
// access the events variable from the index.html
{{ .events }}
关于global-variables - 使用 Hugo,如何从基本文件中定义的部分文件访问变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42124735/