diff options
| author | Tyler Kropp <tyler@tylerkropp.xyz> | 2022-05-02 16:55:34 -0400 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-05-02 14:55:34 -0600 | 
| commit | e84e19a04eefccc743f0d397efe49ff42626f4b3 (patch) | |
| tree | 2941ffe7c7e49572fed9b86cedc7da8c2bdfac76 /modules/caddyhttp/templates/tplcontext.go | |
| parent | 4a223f52038cb77bbf97ca3f3345550dea4e12d8 (diff) | |
templates: Add custom template function registration (#4757)
* Add custom template function registration
* Rename TemplateFunctions to CustomFunctions
* Add documentation
* Document CustomFunctions interface
* Preallocate custom functions map list
* Fix interface name in error message
Diffstat (limited to 'modules/caddyhttp/templates/tplcontext.go')
| -rw-r--r-- | modules/caddyhttp/templates/tplcontext.go | 14 | 
1 files changed, 10 insertions, 4 deletions
diff --git a/modules/caddyhttp/templates/tplcontext.go b/modules/caddyhttp/templates/tplcontext.go index 4f3cbf5..7843455 100644 --- a/modules/caddyhttp/templates/tplcontext.go +++ b/modules/caddyhttp/templates/tplcontext.go @@ -40,10 +40,11 @@ import (  // TemplateContext is the TemplateContext with which HTTP templates are executed.  type TemplateContext struct { -	Root       http.FileSystem -	Req        *http.Request -	Args       []interface{} // defined by arguments to funcInclude -	RespHeader WrappedHeader +	Root        http.FileSystem +	Req         *http.Request +	Args        []interface{} // defined by arguments to funcInclude +	RespHeader  WrappedHeader +	CustomFuncs []template.FuncMap // functions added by plugins  	config *Templates  	tpl    *template.Template @@ -62,6 +63,11 @@ func (c *TemplateContext) NewTemplate(tplName string) *template.Template {  	// add sprig library  	c.tpl.Funcs(sprigFuncMap) +	// add all custom functions +	for _, funcMap := range c.CustomFuncs { +		c.tpl.Funcs(funcMap) +	} +  	// add our own library  	c.tpl.Funcs(template.FuncMap{  		"include":          c.funcInclude,  | 
