tpl: Add template function namespaces

This commit moves almost all of the template functions into separate
packages under tpl/ and adds a namespace framework.  All changes should
be backward compatible for end users, as all existing function names in
the template funcMap are left intact.

Seq and DoArithmatic have been moved out of the helpers package and into
template namespaces.

Most of the tests involved have been refactored, and many new tests have
been written.  There's still work to do, but this is a big improvement.

I got a little overzealous and added some new functions along the way:

- strings.Contains
- strings.ContainsAny
- strings.HasSuffix
- strings.TrimPrefix
- strings.TrimSuffix

Documentation is forthcoming.

Fixes #3042
This commit is contained in:
Cameron Moore
2017-03-13 17:55:02 -05:00
committed by Bjørn Erik Pedersen
parent 154e18ddb9
commit de7c32a1a8
48 changed files with 7069 additions and 5268 deletions

View File

@@ -20,17 +20,43 @@ import (
texttemplate "text/template"
bp "github.com/spf13/hugo/bufferpool"
"image"
"github.com/spf13/hugo/deps"
"github.com/spf13/hugo/tpl/collections"
"github.com/spf13/hugo/tpl/crypto"
"github.com/spf13/hugo/tpl/data"
"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"
hstrings "github.com/spf13/hugo/tpl/strings"
"github.com/spf13/hugo/tpl/time"
"github.com/spf13/hugo/tpl/transform"
"github.com/spf13/hugo/tpl/urls"
)
// Some of the template funcs are'nt entirely stateless.
type templateFuncster struct {
funcMap template.FuncMap
cachedPartials partialCache
image *imageHandler
// Namespaces
collections *collections.Namespace
crypto *crypto.Namespace
data *data.Namespace
encoding *encoding.Namespace
images *images.Namespace
inflect *inflect.Namespace
lang *lang.Namespace
math *math.Namespace
os *os.Namespace
safe *safe.Namespace
strings *hstrings.Namespace
time *time.Namespace
transform *transform.Namespace
urls *urls.Namespace
*deps.Deps
}
@@ -39,7 +65,22 @@ func newTemplateFuncster(deps *deps.Deps) *templateFuncster {
return &templateFuncster{
Deps: deps,
cachedPartials: partialCache{p: make(map[string]interface{})},
image: &imageHandler{fs: deps.Fs, imageConfigCache: map[string]image.Config{}},
// Namespaces
collections: collections.New(deps),
crypto: crypto.New(),
data: data.New(deps),
encoding: encoding.New(),
images: images.New(deps),
inflect: inflect.New(),
lang: lang.New(deps),
math: math.New(),
os: os.New(deps),
safe: safe.New(),
strings: hstrings.New(deps),
time: time.New(),
transform: transform.New(deps),
urls: urls.New(deps),
}
}