.Site.Data 值在范围块内不可访问

.Site.Data values are not accessible inside a range block

所以我刚刚注意到,当我尝试从 .Site 本身访问某些内容时,它位于 range 中,阻止了它 returns作为 null/blank.

这是一个例子:

<div class="row weekday">
  {{ .Site.Data.company.social_media.whatsapp }}
  {{ range $entry := sort .Site.Data.events "order" "asc" }}
  <div class="col-sm-6 col-md-4 col-lg-4">
    {{ .Site.Data.company.social_media.whatsapp }}
    {{ partial "events_detail.html" (dict "entry" $entry) }}
  </div>
  {{ end }}
</div>

第一个 .Site.Data.company.social_media.whatsapp(在范围之前)呈现 phone 数字。

第二个 .Site.Data.company.social_media.whatsapp(范围之后)不呈现任何内容。

同样的行为发生在部分 events_detail.html 中。如果我尝试从部分范围内访问 .Site,它会呈现空值。我也尝试在 (dict ...) 上传递它,但运气不好。

我在这里错过了什么?

我不知道 Hugo 如何管理点的范围。你在这里阅读了关于这个(和其他重要概念)的更深入的解释:

https://regisphilibert.com/blog/2018/02/hugo-the-scope-the-context-and-the-dot/

在简历中是(摘自上面的link):

The root context, the one available to you in your baseof.html and layouts will always be the Page context. Basically everything you need to build this page is in that dot. .Title, .Permalink, .Resources, you name it.

Even your site’s informations is stored in the page context with .Site ready for the taking.

But in Go Template the minute you step into a function you lose that context as your precious dot or context is replaced by the function’s own… dot.

(...)

Same goes here, once you’ve opened an iteration with range the context is the whatever item the cursor is pointing to at the moment. You will lose your page context in favor of the the range context.

{{ range .Data.Pages }}
    {{/* Here the dot is that one page 'at cursor'. */}} 
    {{ .Permalink }}
{{ end }}

Luckily Hugo stores the root context of a template file in a $ so no matter how deeply nested you are within with or range, you can always retrieve the top context. In basic template file, it will generally be your page.

{{ with .Title }}
    {{/* Dot is .Title */}} 
    <h1>{{ . }}</h1>
    {{/* $ is the top level page */}} 
    <h3>From {{ $.Title }}</h3>
{{ end }}