Commit f28a159b authored by Matt Holt's avatar Matt Holt Committed by GitHub

Merge pull request #1591 from tw4452852/template_funcs

template: support custom functions
parents 236341f7 f77a7a80
...@@ -244,6 +244,9 @@ func (c Context) Markdown(filename string) (string, error) { ...@@ -244,6 +244,9 @@ func (c Context) Markdown(filename string) (string, error) {
return string(markdown), nil return string(markdown), nil
} }
// TemplateFuncs contains user defined functions
var TemplateFuncs = template.FuncMap{}
// ContextInclude opens filename using fs and executes a template with the context ctx. // ContextInclude opens filename using fs and executes a template with the context ctx.
// This does the same thing that Context.Include() does, but with the ability to provide // This does the same thing that Context.Include() does, but with the ability to provide
// your own context so that the included files can have access to additional fields your // your own context so that the included files can have access to additional fields your
...@@ -261,7 +264,8 @@ func ContextInclude(filename string, ctx interface{}, fs http.FileSystem) (strin ...@@ -261,7 +264,8 @@ func ContextInclude(filename string, ctx interface{}, fs http.FileSystem) (strin
return "", err return "", err
} }
tpl, err := template.New(filename).Parse(string(body)) tpl := template.New(filename).Funcs(TemplateFuncs)
tpl, err = tpl.Parse(string(body))
if err != nil { if err != nil {
return "", err return "", err
} }
......
...@@ -44,6 +44,9 @@ func (t Templates) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error ...@@ -44,6 +44,9 @@ func (t Templates) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
tpl.Delims(rule.Delims[0], rule.Delims[1]) tpl.Delims(rule.Delims[0], rule.Delims[1])
} }
// Add custom functions
tpl.Funcs(httpserver.TemplateFuncs)
// Build the template // Build the template
templatePath := filepath.Join(t.Root, fpath) templatePath := filepath.Join(t.Root, fpath)
tpl, err := tpl.ParseFiles(templatePath) tpl, err := tpl.ParseFiles(templatePath)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment