2016-02-24 18:52:11 -05:00
|
|
|
// Copyright 2016 The Hugo Authors. All rights reserved.
|
2015-04-05 15:03:16 -04:00
|
|
|
//
|
2016-03-10 14:59:41 -05:00
|
|
|
// Portions Copyright The Go Authors.
|
|
|
|
|
2015-11-23 22:16:36 -05:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
2015-04-05 15:03:16 -04:00
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2015-11-23 22:16:36 -05:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2015-04-05 15:03:16 -04:00
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-02-17 07:30:50 -05:00
|
|
|
package tplimpl
|
2015-04-05 15:03:16 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
2016-03-14 00:17:42 -04:00
|
|
|
"sync"
|
2015-05-26 06:33:32 -04:00
|
|
|
|
2017-03-13 18:55:02 -04:00
|
|
|
"github.com/spf13/cast"
|
2017-04-30 05:34:45 -04:00
|
|
|
"github.com/spf13/hugo/tpl/internal"
|
|
|
|
|
|
|
|
// Init the namespaces
|
2017-04-30 15:52:56 -04:00
|
|
|
_ "github.com/spf13/hugo/tpl/collections"
|
2017-04-30 13:33:19 -04:00
|
|
|
_ "github.com/spf13/hugo/tpl/compare"
|
2017-04-30 16:19:49 -04:00
|
|
|
_ "github.com/spf13/hugo/tpl/crypto"
|
2017-04-30 13:11:18 -04:00
|
|
|
_ "github.com/spf13/hugo/tpl/data"
|
2017-04-30 16:32:40 -04:00
|
|
|
_ "github.com/spf13/hugo/tpl/encoding"
|
2017-04-30 16:36:26 -04:00
|
|
|
_ "github.com/spf13/hugo/tpl/images"
|
2017-04-30 16:43:26 -04:00
|
|
|
_ "github.com/spf13/hugo/tpl/inflect"
|
2017-04-30 05:34:45 -04:00
|
|
|
_ "github.com/spf13/hugo/tpl/lang"
|
2017-04-30 11:45:56 -04:00
|
|
|
_ "github.com/spf13/hugo/tpl/math"
|
2017-04-30 16:52:47 -04:00
|
|
|
_ "github.com/spf13/hugo/tpl/os"
|
2017-04-30 17:06:28 -04:00
|
|
|
_ "github.com/spf13/hugo/tpl/safe"
|
2017-04-30 12:41:13 -04:00
|
|
|
_ "github.com/spf13/hugo/tpl/strings"
|
2017-04-30 17:10:46 -04:00
|
|
|
_ "github.com/spf13/hugo/tpl/time"
|
2017-04-30 17:16:54 -04:00
|
|
|
_ "github.com/spf13/hugo/tpl/transform"
|
2017-04-30 17:33:14 -04:00
|
|
|
_ "github.com/spf13/hugo/tpl/urls"
|
2017-03-13 18:55:02 -04:00
|
|
|
)
|
2016-10-10 18:03:30 -04:00
|
|
|
|
|
|
|
// Get retrieves partial output from the cache based upon the partial name.
|
|
|
|
// If the partial is not found in the cache, the partial is rendered and added
|
|
|
|
// to the cache.
|
2017-03-27 14:43:49 -04:00
|
|
|
func (t *templateFuncster) Get(key, name string, context interface{}) (p interface{}, err error) {
|
2016-10-10 18:03:30 -04:00
|
|
|
var ok bool
|
|
|
|
|
2017-01-10 04:55:03 -05:00
|
|
|
t.cachedPartials.RLock()
|
|
|
|
p, ok = t.cachedPartials.p[key]
|
|
|
|
t.cachedPartials.RUnlock()
|
2016-10-10 18:03:30 -04:00
|
|
|
|
|
|
|
if ok {
|
2017-03-27 14:43:49 -04:00
|
|
|
return
|
2016-10-10 18:03:30 -04:00
|
|
|
}
|
|
|
|
|
2017-01-10 04:55:03 -05:00
|
|
|
t.cachedPartials.Lock()
|
|
|
|
if p, ok = t.cachedPartials.p[key]; !ok {
|
|
|
|
t.cachedPartials.Unlock()
|
2017-03-27 14:43:49 -04:00
|
|
|
p, err = t.partial(name, context)
|
2017-01-10 04:55:03 -05:00
|
|
|
|
|
|
|
t.cachedPartials.Lock()
|
|
|
|
t.cachedPartials.p[key] = p
|
2017-01-17 13:51:24 -05:00
|
|
|
|
2016-10-10 18:03:30 -04:00
|
|
|
}
|
2017-01-10 04:55:03 -05:00
|
|
|
t.cachedPartials.Unlock()
|
2016-10-10 18:03:30 -04:00
|
|
|
|
2017-03-27 14:43:49 -04:00
|
|
|
return
|
2016-10-10 18:03:30 -04:00
|
|
|
}
|
|
|
|
|
2017-03-13 18:55:02 -04:00
|
|
|
// partialCache represents a cache of partials protected by a mutex.
|
|
|
|
type partialCache struct {
|
|
|
|
sync.RWMutex
|
|
|
|
p map[string]interface{}
|
|
|
|
}
|
|
|
|
|
2016-10-10 18:03:30 -04:00
|
|
|
// partialCached executes and caches partial templates. An optional variant
|
|
|
|
// string parameter (a string slice actually, but be only use a variadic
|
|
|
|
// argument to make it optional) can be passed so that a given partial can have
|
|
|
|
// multiple uses. The cache is created with name+variant as the key.
|
2017-03-27 14:43:49 -04:00
|
|
|
func (t *templateFuncster) partialCached(name string, context interface{}, variant ...string) (interface{}, error) {
|
2016-10-10 18:03:30 -04:00
|
|
|
key := name
|
|
|
|
if len(variant) > 0 {
|
|
|
|
for i := 0; i < len(variant); i++ {
|
|
|
|
key += variant[i]
|
|
|
|
}
|
|
|
|
}
|
2017-01-10 04:55:03 -05:00
|
|
|
return t.Get(key, name, context)
|
2016-10-10 18:03:30 -04:00
|
|
|
}
|
|
|
|
|
2017-01-10 04:55:03 -05:00
|
|
|
func (t *templateFuncster) initFuncMap() {
|
2017-01-09 19:36:59 -05:00
|
|
|
funcMap := template.FuncMap{
|
2017-03-13 18:55:02 -04:00
|
|
|
// Namespaces
|
|
|
|
//"time": t.time.Namespace,
|
2016-10-10 18:03:30 -04:00
|
|
|
"int": func(v interface{}) (int, error) { return cast.ToIntE(v) },
|
2017-03-27 14:43:49 -04:00
|
|
|
"partial": t.partial,
|
2017-01-10 04:55:03 -05:00
|
|
|
"partialCached": t.partialCached,
|
2017-03-07 15:11:03 -05:00
|
|
|
"print": fmt.Sprint,
|
|
|
|
"printf": fmt.Sprintf,
|
|
|
|
"println": fmt.Sprintln,
|
2017-03-13 18:55:02 -04:00
|
|
|
"string": func(v interface{}) (string, error) { return cast.ToStringE(v) },
|
|
|
|
"urlize": t.PathSpec.URLize,
|
2017-04-30 05:34:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|
2017-01-09 19:36:59 -05:00
|
|
|
|
2017-01-10 04:55:03 -05:00
|
|
|
t.funcMap = funcMap
|
2017-04-15 05:33:53 -04:00
|
|
|
t.Tmpl.(*templateHandler).setFuncs(funcMap)
|
2015-04-05 15:03:16 -04:00
|
|
|
}
|