github.com/fighterlyt/hugo@v0.47.1/tpl/tplimpl/template_funcs.go (about)

     1  // Copyright 2017-present The Hugo Authors. All rights reserved.
     2  //
     3  // Portions Copyright The Go Authors.
     4  
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  // http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package tplimpl
    17  
    18  import (
    19  	"html/template"
    20  
    21  	"github.com/gohugoio/hugo/deps"
    22  
    23  	"github.com/gohugoio/hugo/tpl/internal"
    24  
    25  	// Init the namespaces
    26  	_ "github.com/gohugoio/hugo/tpl/cast"
    27  	_ "github.com/gohugoio/hugo/tpl/collections"
    28  	_ "github.com/gohugoio/hugo/tpl/compare"
    29  	_ "github.com/gohugoio/hugo/tpl/crypto"
    30  	_ "github.com/gohugoio/hugo/tpl/data"
    31  	_ "github.com/gohugoio/hugo/tpl/encoding"
    32  	_ "github.com/gohugoio/hugo/tpl/fmt"
    33  	_ "github.com/gohugoio/hugo/tpl/images"
    34  	_ "github.com/gohugoio/hugo/tpl/inflect"
    35  	_ "github.com/gohugoio/hugo/tpl/lang"
    36  	_ "github.com/gohugoio/hugo/tpl/math"
    37  	_ "github.com/gohugoio/hugo/tpl/os"
    38  	_ "github.com/gohugoio/hugo/tpl/partials"
    39  	_ "github.com/gohugoio/hugo/tpl/path"
    40  	_ "github.com/gohugoio/hugo/tpl/resources"
    41  	_ "github.com/gohugoio/hugo/tpl/safe"
    42  	_ "github.com/gohugoio/hugo/tpl/strings"
    43  	_ "github.com/gohugoio/hugo/tpl/templates"
    44  	_ "github.com/gohugoio/hugo/tpl/time"
    45  	_ "github.com/gohugoio/hugo/tpl/transform"
    46  	_ "github.com/gohugoio/hugo/tpl/urls"
    47  )
    48  
    49  func createFuncMap(d *deps.Deps) map[string]interface{} {
    50  	funcMap := template.FuncMap{}
    51  
    52  	// Merge the namespace funcs
    53  	for _, nsf := range internal.TemplateFuncsNamespaceRegistry {
    54  		ns := nsf(d)
    55  		if _, exists := funcMap[ns.Name]; exists {
    56  			panic(ns.Name + " is a duplicate template func")
    57  		}
    58  		funcMap[ns.Name] = ns.Context
    59  		for _, mm := range ns.MethodMappings {
    60  			for _, alias := range mm.Aliases {
    61  				if _, exists := funcMap[alias]; exists {
    62  					panic(alias + " is a duplicate template func")
    63  				}
    64  				funcMap[alias] = mm.Method
    65  			}
    66  
    67  		}
    68  
    69  	}
    70  
    71  	return funcMap
    72  
    73  }
    74  func (t *templateFuncster) initFuncMap(funcMap template.FuncMap) {
    75  	t.funcMap = funcMap
    76  	t.Tmpl.(*templateHandler).setFuncs(funcMap)
    77  }