aboutsummaryrefslogtreecommitdiffhomepage
path: root/hugolib
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <[email protected]>2024-11-13 11:07:32 +0100
committerBjørn Erik Pedersen <[email protected]>2024-11-13 12:41:16 +0100
commita7df536a52912f34d7d20c970c38590bf5e0c513 (patch)
tree2773e4660f458f32d19784cfb5f557f3b7ffb6e2 /hugolib
parent3477d9fcec39d17f99cbc891e337658e8660f5db (diff)
downloadhugo-a7df536a52912f34d7d20c970c38590bf5e0c513.tar.gz
hugo-a7df536a52912f34d7d20c970c38590bf5e0c513.zip
Add site.Store and hugo.Store and Shortcode.Store
Closes #13021
Diffstat (limited to 'hugolib')
-rw-r--r--hugolib/page_test.go49
-rw-r--r--hugolib/shortcode.go34
-rw-r--r--hugolib/site.go6
3 files changed, 80 insertions, 9 deletions
diff --git a/hugolib/page_test.go b/hugolib/page_test.go
index bdd1be6f7..39a16d948 100644
--- a/hugolib/page_test.go
+++ b/hugolib/page_test.go
@@ -1893,3 +1893,52 @@ func TestRenderWithoutArgument(t *testing.T) {
b.Assert(err, qt.IsNotNil)
}
+
+// Issue #13021
+func TestAllStores(t *testing.T) {
+ t.Parallel()
+
+ files := `
+-- hugo.toml --
+disableKinds = ["taxonomy", "term", "page", "section"]
+disableLiveReload = true
+-- content/_index.md --
+---
+title: "Home"
+---
+{{< s >}}
+-- layouts/shortcodes/s.html --
+{{ if not (.Store.Get "Shortcode") }}{{ .Store.Set "Shortcode" (printf "sh-%s" $.Page.Title) }}{{ end }}
+Shortcode: {{ .Store.Get "Shortcode" }}|
+-- layouts/index.html --
+{{ .Content }}
+{{ if not (.Store.Get "Page") }}{{ .Store.Set "Page" (printf "p-%s" $.Title) }}{{ end }}
+{{ if not (hugo.Store.Get "Hugo") }}{{ hugo.Store.Set "Hugo" (printf "h-%s" $.Title) }}{{ end }}
+{{ if not (site.Store.Get "Site") }}{{ site.Store.Set "Site" (printf "s-%s" $.Title) }}{{ end }}
+Page: {{ .Store.Get "Page" }}|
+Hugo: {{ hugo.Store.Get "Hugo" }}|
+Site: {{ site.Store.Get "Site" }}|
+`
+
+ b := TestRunning(t, files)
+
+ b.AssertFileContent("public/index.html",
+ `
+Shortcode: sh-Home|
+Page: p-Home|
+Site: s-Home|
+Hugo: h-Home|
+`,
+ )
+
+ b.EditFileReplaceAll("content/_index.md", "Home", "Homer").Build()
+
+ b.AssertFileContent("public/index.html",
+ `
+Shortcode: sh-Homer|
+Page: p-Homer|
+Site: s-Home|
+Hugo: h-Home|
+`,
+ )
+}
diff --git a/hugolib/shortcode.go b/hugolib/shortcode.go
index 8a478c9df..69e891adb 100644
--- a/hugolib/shortcode.go
+++ b/hugolib/shortcode.go
@@ -43,9 +43,10 @@ import (
)
var (
- _ urls.RefLinker = (*ShortcodeWithPage)(nil)
- _ types.Unwrapper = (*ShortcodeWithPage)(nil)
- _ text.Positioner = (*ShortcodeWithPage)(nil)
+ _ urls.RefLinker = (*ShortcodeWithPage)(nil)
+ _ types.Unwrapper = (*ShortcodeWithPage)(nil)
+ _ text.Positioner = (*ShortcodeWithPage)(nil)
+ _ maps.StoreProvider = (*ShortcodeWithPage)(nil)
)
// ShortcodeWithPage is the "." context in a shortcode template.
@@ -72,7 +73,7 @@ type ShortcodeWithPage struct {
posOffset int
pos text.Position
- scratch *maps.Scratch
+ store *maps.Scratch
}
// InnerDeindent returns the (potentially de-indented) inner content of the shortcode.
@@ -124,13 +125,19 @@ func (scp *ShortcodeWithPage) RelRef(args map[string]any) (string, error) {
return scp.Page.RelRefFrom(args, scp)
}
+// Store returns this shortcode's Store.
+func (scp *ShortcodeWithPage) Store() *maps.Scratch {
+ if scp.store == nil {
+ scp.store = maps.NewScratch()
+ }
+ return scp.store
+}
+
// Scratch returns a scratch-pad scoped for this shortcode. This can be used
// as a temporary storage for variables, counters etc.
+// Deprecated: Use Store instead. Note that from the templates this should be considered a "soft deprecation".
func (scp *ShortcodeWithPage) Scratch() *maps.Scratch {
- if scp.scratch == nil {
- scp.scratch = maps.NewScratch()
- }
- return scp.scratch
+ return scp.Store()
}
// Get is a convenience method to look up shortcode parameters by its key.
@@ -399,7 +406,16 @@ func doRenderShortcode(
hasVariants = hasVariants || more
}
- data := &ShortcodeWithPage{Ordinal: sc.ordinal, posOffset: sc.pos, indentation: sc.indentation, Params: sc.params, Page: newPageForShortcode(p), Parent: parent, Name: sc.name}
+ data := &ShortcodeWithPage{
+ Ordinal: sc.ordinal,
+ posOffset: sc.pos,
+ indentation: sc.indentation,
+ Params: sc.params,
+ Page: newPageForShortcode(p),
+ Parent: parent,
+ Name: sc.name,
+ }
+
if sc.params != nil {
data.IsNamedParams = reflect.TypeOf(sc.params).Kind() == reflect.Map
}
diff --git a/hugolib/site.go b/hugolib/site.go
index c5a4956e2..c434ff2b4 100644
--- a/hugolib/site.go
+++ b/hugolib/site.go
@@ -95,6 +95,7 @@ type Site struct {
language *langs.Language
languagei int
pageMap *pageMap
+ store *maps.Scratch
// The owning container.
h *HugoSites
@@ -248,6 +249,7 @@ func NewHugoSites(cfg deps.DepsCfg) (*HugoSites, error) {
language: language,
languagei: i,
frontmatterHandler: frontmatterHandler,
+ store: maps.NewScratch(),
}
if i == 0 {
@@ -614,6 +616,10 @@ func (s *Site) AllRegularPages() page.Pages {
return s.h.RegularPages()
}
+func (s *Site) Store() *maps.Scratch {
+ return s.store
+}
+
func (s *Site) CheckReady() {
if s.state != siteStateReady {
panic("this method cannot be called before the site is fully initialized")