mirror of
https://github.com/gohugoio/hugo.git
synced 2025-09-01 22:42:45 +02:00
tpl: Add TemplateFuncsNamespaceRegistry
As a first step to remove the hard ties between `tplimpl` and the different namespace packages. The `lang` package is used as the first example use case. See #3042
This commit is contained in:
@@ -27,7 +27,6 @@ import (
|
||||
"github.com/spf13/hugo/tpl/encoding"
|
||||
"github.com/spf13/hugo/tpl/images"
|
||||
"github.com/spf13/hugo/tpl/inflect"
|
||||
"github.com/spf13/hugo/tpl/lang"
|
||||
"github.com/spf13/hugo/tpl/math"
|
||||
"github.com/spf13/hugo/tpl/os"
|
||||
"github.com/spf13/hugo/tpl/safe"
|
||||
@@ -49,7 +48,6 @@ type templateFuncster struct {
|
||||
encoding *encoding.Namespace
|
||||
images *images.Namespace
|
||||
inflect *inflect.Namespace
|
||||
lang *lang.Namespace
|
||||
math *math.Namespace
|
||||
os *os.Namespace
|
||||
safe *safe.Namespace
|
||||
@@ -73,7 +71,6 @@ func newTemplateFuncster(deps *deps.Deps) *templateFuncster {
|
||||
encoding: encoding.New(),
|
||||
images: images.New(deps),
|
||||
inflect: inflect.New(),
|
||||
lang: lang.New(deps),
|
||||
math: math.New(),
|
||||
os: os.New(deps),
|
||||
safe: safe.New(),
|
||||
|
@@ -22,6 +22,10 @@ import (
|
||||
|
||||
"github.com/spf13/cast"
|
||||
"github.com/spf13/hugo/tpl/compare"
|
||||
"github.com/spf13/hugo/tpl/internal"
|
||||
|
||||
// Init the namespaces
|
||||
_ "github.com/spf13/hugo/tpl/lang"
|
||||
)
|
||||
|
||||
// Get retrieves partial output from the cache based upon the partial name.
|
||||
@@ -181,8 +185,18 @@ func (t *templateFuncster) initFuncMap() {
|
||||
"upper": t.strings.ToUpper,
|
||||
"urlize": t.PathSpec.URLize,
|
||||
"where": t.collections.Where,
|
||||
"i18n": t.lang.Translate,
|
||||
"T": t.lang.T,
|
||||
}
|
||||
|
||||
// Merge the namespace funcs
|
||||
for _, nsf := range internal.TemplateFuncsNamespaceRegistry {
|
||||
ns := nsf(t.Deps)
|
||||
// TODO(bep) namespace ns.Context is a dummy func just to make this work.
|
||||
// Consider if we can add this context to the rendering context in an easy
|
||||
// way to make this cleaner. Maybe.
|
||||
funcMap[ns.Name] = ns.Context
|
||||
for k, v := range ns.Aliases {
|
||||
funcMap[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
t.funcMap = funcMap
|
||||
|
@@ -70,6 +70,7 @@ func TestFuncsInTemplate(t *testing.T) {
|
||||
|
||||
// Add the examples from the docs: As a smoke test and to make sure the examples work.
|
||||
// TODO(bep): docs: fix title example
|
||||
// TODO(bep) namespace remove when done
|
||||
in :=
|
||||
`absLangURL: {{ "index.html" | absLangURL }}
|
||||
absURL: {{ "http://gohugo.io/" | absURL }}
|
||||
|
Reference in New Issue
Block a user