"github.com/gohugoio/hugo/common/hcontext"
"github.com/gohugoio/hugo/common/hexec"
"github.com/gohugoio/hugo/common/loggers"
+ "github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/hugo/hugofs/files"
"github.com/spf13/afero"
vendorInfo string
)
+var _ maps.StoreProvider = (*HugoInfo)(nil)
+
// HugoInfo contains information about the current Hugo environment
type HugoInfo struct {
CommitHash string
conf ConfigProvider
deps []*Dependency
+ store *maps.Scratch
+
// Context gives access to some of the context scoped variables.
Context Context
}
return i.deps
}
+func (i HugoInfo) Store() *maps.Scratch {
+ return i.store
+}
+
// Deprecated: Use hugo.IsMultihost instead.
func (i HugoInfo) IsMultiHost() bool {
Deprecate("hugo.IsMultiHost", "Use hugo.IsMultihost instead.", "v0.124.0")
Environment: conf.Environment(),
conf: conf,
deps: deps,
+ store: maps.NewScratch(),
GoVersion: goVersion,
}
}
"github.com/gohugoio/hugo/common/math"
)
-// Scratch is a writable context used for stateful operations in Page/Node rendering.
+type StoreProvider interface {
+ // Store returns a Scratch that can be used to store temporary state.
+ // Store is not reset on server rebuilds.
+ Store() *Scratch
+}
+
+// Scratch is a writable context used for stateful build operations
type Scratch struct {
values map[string]any
mu sync.RWMutex
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|
+`,
+ )
+}
)
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.
posOffset int
pos text.Position
- scratch *maps.Scratch
+ store *maps.Scratch
}
// InnerDeindent returns the (potentially de-indented) inner content of the shortcode.
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.
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
}
language *langs.Language
languagei int
pageMap *pageMap
+ store *maps.Scratch
// The owning container.
h *HugoSites
language: language,
languagei: i,
frontmatterHandler: frontmatterHandler,
+ store: maps.NewScratch(),
}
if i == 0 {
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")
// Deprecated: From Hugo v0.138.0 this is just an alias for Store.
Scratch() *maps.Scratch
- // Store returns a Scratch that can be used to store temporary state.
- // In contrast to Scratch(), this Scratch is not reset on server rebuilds.
- Store() *maps.Scratch
+ maps.StoreProvider
RelatedKeywordsProvider
// Deprecated: Use .Site.Home.OutputFormats.Get "rss" instead.
RSSLink() template.URL
+ maps.StoreProvider
+
// For internal use only.
// This will panic if the site is not fully initialized.
// This is typically used to inform the user in the content adapter templates,
return s.s.RSSLink()
}
+func (s *siteWrapper) Store() *maps.Scratch {
+ return s.s.Store()
+}
+
// For internal use only.
func (s *siteWrapper) ForEeachIdentityByName(name string, f func(identity.Identity) bool) {
s.s.(identity.ForEeachIdentityByNameProvider).ForEeachIdentityByName(name, f)
return ""
}
+func (s testSite) Store() *maps.Scratch {
+ return maps.NewScratch()
+}
+
func (s testSite) CheckReady() {
}