Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 14, 05:15 UTC
templates: Add env function (closes #3237)
Matthew Holt committed
Apr 7, 2020 at 12:26 UTC
b1ce9d4db7f7ee6d8b83a29675efc10550919d11
1 file changed
+9
-3
modules/caddyhttp/templates/tplcontext.go
+9
-3
@@ -20,6 +20,7 @@ import (
20
"io"
21
"net"
22
"net/http"
23
+ "os"
24
"path"
25
"strconv"
26
"strings"
@@ -150,6 +151,7 @@ func (c templateContext) executeTemplateInBuffer(tplName string, buf *bytes.Buff
151
"markdown": c.funcMarkdown,
152
"splitFrontMatter": c.funcSplitFrontMatter,
153
"listFiles": c.funcListFiles,
154
+ "env": c.funcEnv,
155
})
156
157
parsedTpl, err := tpl.Parse(buf.String())
@@ -162,6 +164,10 @@ func (c templateContext) executeTemplateInBuffer(tplName string, buf *bytes.Buff
164
return parsedTpl.Execute(buf, c)
165
}
166
167
+func (templateContext) funcEnv(varName string) string {
168
+ return os.Getenv(varName)
169
+}
170
+
171
// Cookie gets the value of a cookie with name name.
172
func (c templateContext) Cookie(name string) string {
173
cookies := c.Req.Cookies()
@@ -198,7 +204,7 @@ func (c templateContext) Host() (string, error) {
204
205
// funcStripHTML returns s without HTML tags. It is fairly naive
206
// but works with most valid HTML inputs.
201
-func (c templateContext) funcStripHTML(s string) string {
207
+func (templateContext) funcStripHTML(s string) string {
208
var buf bytes.Buffer
209
var inTag, inQuotes bool
210
var tagStart int
@@ -231,7 +237,7 @@ func (c templateContext) funcStripHTML(s string) string {
237
238
// funcMarkdown renders the markdown body as HTML. The resulting
239
// HTML is NOT escaped so that it can be rendered as HTML.
234
-func (c templateContext) funcMarkdown(input interface{}) (string, error) {
240
+func (templateContext) funcMarkdown(input interface{}) (string, error) {
241
inputStr := toString(input)
242
243
md := goldmark.New(
@@ -265,7 +271,7 @@ func (c templateContext) funcMarkdown(input interface{}) (string, error) {
271
// splitFrontMatter parses front matter out from the beginning of input,
272
// and returns the separated key-value pairs and the body/content. input
273
// must be a "stringy" value.
268
-func (c templateContext) funcSplitFrontMatter(input interface{}) (parsedMarkdownDoc, error) {
274
+func (templateContext) funcSplitFrontMatter(input interface{}) (parsedMarkdownDoc, error) {
275
meta, body, err := extractFrontMatter(toString(input))
276
if err != nil {
277
return parsedMarkdownDoc{}, err