diff options
author | Matthew Holt <mholt@users.noreply.github.com> | 2020-04-07 12:26:08 -0600 |
---|---|---|
committer | Matthew Holt <mholt@users.noreply.github.com> | 2020-04-07 12:26:08 -0600 |
commit | b1ce9d4db7f7ee6d8b83a29675efc10550919d11 (patch) | |
tree | be2d58e0b425e48fa7b33c7d82c0445049af7bbc /modules | |
parent | 61679b74f5975e6a5f1eeab4e209148aee9bcd5a (diff) |
templates: Add env function (closes #3237)
Diffstat (limited to 'modules')
-rw-r--r-- | modules/caddyhttp/templates/tplcontext.go | 12 |
1 files 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 |