mirror of
https://github.com/caddyserver/caddy.git
synced 2025-10-19 07:43:17 +00:00
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
This commit is contained in:
parent
4a223f5203
commit
e84e19a04e
2 changed files with 38 additions and 8 deletions
|
@ -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,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue