mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-29 07:03:48 +03:00
templates: Add env function (closes #3237)
This commit is contained in:
parent
61679b74f5
commit
b1ce9d4db7
1 changed files with 9 additions and 3 deletions
|
@ -20,6 +20,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -150,6 +151,7 @@ func (c templateContext) executeTemplateInBuffer(tplName string, buf *bytes.Buff
|
||||||
"markdown": c.funcMarkdown,
|
"markdown": c.funcMarkdown,
|
||||||
"splitFrontMatter": c.funcSplitFrontMatter,
|
"splitFrontMatter": c.funcSplitFrontMatter,
|
||||||
"listFiles": c.funcListFiles,
|
"listFiles": c.funcListFiles,
|
||||||
|
"env": c.funcEnv,
|
||||||
})
|
})
|
||||||
|
|
||||||
parsedTpl, err := tpl.Parse(buf.String())
|
parsedTpl, err := tpl.Parse(buf.String())
|
||||||
|
@ -162,6 +164,10 @@ func (c templateContext) executeTemplateInBuffer(tplName string, buf *bytes.Buff
|
||||||
return parsedTpl.Execute(buf, c)
|
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.
|
// Cookie gets the value of a cookie with name name.
|
||||||
func (c templateContext) Cookie(name string) string {
|
func (c templateContext) Cookie(name string) string {
|
||||||
cookies := c.Req.Cookies()
|
cookies := c.Req.Cookies()
|
||||||
|
@ -198,7 +204,7 @@ func (c templateContext) Host() (string, error) {
|
||||||
|
|
||||||
// funcStripHTML returns s without HTML tags. It is fairly naive
|
// funcStripHTML returns s without HTML tags. It is fairly naive
|
||||||
// but works with most valid HTML inputs.
|
// 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 buf bytes.Buffer
|
||||||
var inTag, inQuotes bool
|
var inTag, inQuotes bool
|
||||||
var tagStart int
|
var tagStart int
|
||||||
|
@ -231,7 +237,7 @@ func (c templateContext) funcStripHTML(s string) string {
|
||||||
|
|
||||||
// funcMarkdown renders the markdown body as HTML. The resulting
|
// funcMarkdown renders the markdown body as HTML. The resulting
|
||||||
// HTML is NOT escaped so that it can be rendered as HTML.
|
// 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)
|
inputStr := toString(input)
|
||||||
|
|
||||||
md := goldmark.New(
|
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,
|
// splitFrontMatter parses front matter out from the beginning of input,
|
||||||
// and returns the separated key-value pairs and the body/content. input
|
// and returns the separated key-value pairs and the body/content. input
|
||||||
// must be a "stringy" value.
|
// 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))
|
meta, body, err := extractFrontMatter(toString(input))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return parsedMarkdownDoc{}, err
|
return parsedMarkdownDoc{}, err
|
||||||
|
|
Loading…
Reference in a new issue