From b1ce9d4db7f7ee6d8b83a29675efc10550919d11 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Tue, 7 Apr 2020 12:26:08 -0600 Subject: templates: Add env function (closes #3237) --- modules/caddyhttp/templates/tplcontext.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/modules/caddyhttp/templates/tplcontext.go b/modules/caddyhttp/templates/tplcontext.go index 2d7b957..85b97f1 100644 --- a/modules/caddyhttp/templates/tplcontext.go +++ b/modules/caddyhttp/templates/tplcontext.go @@ -20,6 +20,7 @@ import ( "io" "net" "net/http" + "os" "path" "strconv" "strings" @@ -150,6 +151,7 @@ func (c templateContext) executeTemplateInBuffer(tplName string, buf *bytes.Buff "markdown": c.funcMarkdown, "splitFrontMatter": c.funcSplitFrontMatter, "listFiles": c.funcListFiles, + "env": c.funcEnv, }) parsedTpl, err := tpl.Parse(buf.String()) @@ -162,6 +164,10 @@ func (c templateContext) executeTemplateInBuffer(tplName string, buf *bytes.Buff return parsedTpl.Execute(buf, c) } +func (templateContext) funcEnv(varName string) string { + return os.Getenv(varName) +} + // Cookie gets the value of a cookie with name name. func (c templateContext) Cookie(name string) string { cookies := c.Req.Cookies() @@ -198,7 +204,7 @@ func (c templateContext) Host() (string, error) { // funcStripHTML returns s without HTML tags. It is fairly naive // but works with most valid HTML inputs. -func (c templateContext) funcStripHTML(s string) string { +func (templateContext) funcStripHTML(s string) string { var buf bytes.Buffer var inTag, inQuotes bool var tagStart int @@ -231,7 +237,7 @@ func (c templateContext) funcStripHTML(s string) string { // funcMarkdown renders the markdown body as HTML. The resulting // HTML is NOT escaped so that it can be rendered as HTML. -func (c templateContext) funcMarkdown(input interface{}) (string, error) { +func (templateContext) funcMarkdown(input interface{}) (string, error) { inputStr := toString(input) md := goldmark.New( @@ -265,7 +271,7 @@ func (c templateContext) funcMarkdown(input interface{}) (string, error) { // splitFrontMatter parses front matter out from the beginning of input, // and returns the separated key-value pairs and the body/content. input // must be a "stringy" value. -func (c templateContext) funcSplitFrontMatter(input interface{}) (parsedMarkdownDoc, error) { +func (templateContext) funcSplitFrontMatter(input interface{}) (parsedMarkdownDoc, error) { meta, body, err := extractFrontMatter(toString(input)) if err != nil { return parsedMarkdownDoc{}, err -- cgit v1.2.3