Replace deprecated site.Data with hugo.Data in tests

This also fixes a subtle server reload issue when changing files inside /data and using the recently added hugo.Data method.
This commit is contained in:
Bjørn Erik Pedersen
2026-03-21 19:09:14 +01:00
parent 182b104571
commit a8fca598e6
8 changed files with 29 additions and 18 deletions
+7 -7
View File
@@ -37,10 +37,10 @@ v1 = "a_v1_theme"
-- themes/mytheme/data/d.toml --
v1 = "d_v1_theme"
-- layouts/home.html --
a: {{ site.Data.a.v1 }}|
b: {{ site.Data.b.v1 }}|
cd: {{ site.Data.c.d.v1 }}|
d: {{ site.Data.d.v1 }}|
a: {{ hugo.Data.a.v1 }}|
b: {{ hugo.Data.b.v1 }}|
cd: {{ hugo.Data.c.d.v1 }}|
d: {{ hugo.Data.d.v1 }}|
`
b := Test(t, files)
@@ -57,8 +57,8 @@ baseURL = "https://example.com"
-- data/MyFolder/MyData.toml --
v1 = "my_v1"
-- layouts/home.html --
{{ site.Data }}
v1: {{ site.Data.MyFolder.MyData.v1 }}|
{{ hugo.Data }}
v1: {{ hugo.Data.MyFolder.MyData.v1 }}|
`
b := Test(t, files)
@@ -75,7 +75,7 @@ disableKinds = ['page','rss','section','sitemap','taxonomy','term']
-- assets/data/foo.toml --
content = "I am assets/data/foo.toml"
-- layouts/home.html --
|{{ site.Data.foo.content }}|
|{{ hugo.Data.foo.content }}|
`
b := Test(t, files)
+4 -4
View File
@@ -229,8 +229,8 @@ talks = [
{ date = 2050-02-13, name = "Future talk 2" },
]
-- layouts/home.html --
{{ $futureTalks := where site.Data.mydata.talks "date" ">" now }}
{{ $pastTalks := where site.Data.mydata.talks "date" "<" now }}
{{ $futureTalks := where hugo.Data.mydata.talks "date" ">" now }}
{{ $pastTalks := where hugo.Data.mydata.talks "date" "<" now }}
{{ $homeDate := site.Home.Date }}
{{ $p1Date := (site.GetPage "p1").Date }}
@@ -239,9 +239,9 @@ Past talks: {{ len $pastTalks }}
Home's Date should be greater than past: {{ gt $homeDate (index $pastTalks 0).date }}
Home's Date should be less than future: {{ lt $homeDate (index $futureTalks 0).date }}
Home's Date should be equal mydata date: {{ eq $homeDate site.Data.mydata.date }}
Home's Date should be equal mydata date: {{ eq $homeDate hugo.Data.mydata.date }}
Home date: {{ $homeDate }}
mydata.date: {{ site.Data.mydata.date }}
mydata.date: {{ hugo.Data.mydata.date }}
Full time: {{ $p1Date | time.Format ":time_full" }}
`
+1 -1
View File
@@ -358,7 +358,7 @@ target = "data/extra"
-- extra-data/test.yaml --
message: Hugo Rocks
-- layouts/home.html --
{{ site.Data.extra.test.message }}
{{ hugo.Data.extra.test.message }}
`
b := Test(t, files)
+1 -1
View File
@@ -1055,7 +1055,7 @@ func (h *HugoSites) processPartialFileEvents(ctx context.Context, l logg.LevelLo
case files.ComponentFolderData:
logger.Println("Data changed", pathInfo.Path())
// This should cover all usage of site.Data.
// This should cover all usage of hugo.Data.
// Currently very coarse grained.
changes = append(changes, siteidentities.Data)
h.init.data.Reset()
+2 -2
View File
@@ -633,8 +633,8 @@ path = "mytheme"
-- layouts/home.html --
i18n s1: {{ i18n "s1" }}|
i18n s2: {{ i18n "s2" }}|
data s1: {{ site.Data.d1.s1 }}|
data s2: {{ site.Data.d1.s2 }}|
data s1: {{ hugo.Data.d1.s1 }}|
data s2: {{ hugo.Data.d1.s2 }}|
title: {{ .Title }}|
-- themes/mytheme/hugo.toml --
[[module.mounts]]
+1 -1
View File
@@ -72,7 +72,7 @@ foo
-- assets/js/include.js --
foo
-- layouts/home.html --
Data: {{ site.Data }}:END
Data: {{ hugo.Data }}:END
Template: {{ templates.Exists "partials/foo.html" }}:END
Resource1: {{ resources.Get "js/include.js" }}:END
+2 -2
View File
@@ -1394,10 +1394,10 @@ title: "Home"
title: "P1"
---
Foo inline: {{< foo.inline >}}{{ site.Data.mydata.foo }}|{{< /foo.inline >}}
Foo inline: {{< foo.inline >}}{{ hugo.Data.mydata.foo }}|{{< /foo.inline >}}
-- layouts/_shortcodes/data.html --
{{ $path := split (.Get 0) "." }}
{{ $data := index site.Data $path }}
{{ $data := index hugo.Data $path }}
Foo: {{ $data }}|
-- layouts/home.html --
Content: {{ .Content }}|
+11
View File
@@ -20,6 +20,8 @@ import (
"github.com/gohugoio/hugo/common/hstore"
"github.com/gohugoio/hugo/common/hugo"
"github.com/gohugoio/hugo/common/version"
"github.com/gohugoio/hugo/identity"
"github.com/gohugoio/hugo/resources/page/siteidentities"
)
var _ hstore.StoreProvider = (*HugoInfo)(nil)
@@ -114,6 +116,15 @@ func (i HugoInfo) IsMultilingual() bool {
return i.opts.Conf.IsMultilingual()
}
// For internal use.
func (s HugoInfo) ForEeachIdentityByName(name string, f func(identity.Identity) bool) {
if id, found := siteidentities.FromString(name); found {
if f(id) {
return
}
}
}
// HugoInfoConfigProvider represents the config options that are relevant for HugoInfo.
type HugoInfoConfigProvider interface {
Environment() string