diff options
author | Patrick Hein <bagbag@users.noreply.github.com> | 2020-07-21 01:17:38 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-20 17:17:38 -0600 |
commit | 2bc30bb780f3b93593a2a9e42db6ab215fe12902 (patch) | |
tree | 5047581931bd12b53b3f460e222a806b12ca185f /modules/caddyhttp/templates | |
parent | 28d870c193f33920cd10e3038151d18173e533ec (diff) |
templates: Implement placeholders function (#3324)
* caddyhttp, httpcaddyfile: Implement placeholders in template
* caddyhttp, httpcaddyfile: Remove support for placeholder shorthands in templates
* Update modules/caddyhttp/templates/templates.go
updates JSON doc
Co-authored-by: Matt Holt <mholt@users.noreply.github.com>
* Update modules/caddyhttp/templates/tplcontext.go
Co-authored-by: Matt Holt <mholt@users.noreply.github.com>
Co-authored-by: Matt Holt <mholt@users.noreply.github.com>
Diffstat (limited to 'modules/caddyhttp/templates')
-rw-r--r-- | modules/caddyhttp/templates/templates.go | 10 | ||||
-rw-r--r-- | modules/caddyhttp/templates/tplcontext.go | 8 |
2 files changed, 18 insertions, 0 deletions
diff --git a/modules/caddyhttp/templates/templates.go b/modules/caddyhttp/templates/templates.go index f5df9be..f9ce1c3 100644 --- a/modules/caddyhttp/templates/templates.go +++ b/modules/caddyhttp/templates/templates.go @@ -64,6 +64,16 @@ func init() { // {{env "VAR_NAME"}} // ``` // +// ##### `placeholder` +// +// Gets an [placeholder variable](/docs/conventions#placeholders). +// The braces (`{}`) have to be omitted. +// +// ``` +// {{placeholder "http.request.uri.path"}} +// {{placeholder "http.error.status_code"}} +// ``` +// // ##### `.Host` // // Returns the hostname portion (no port) of the Host header of the HTTP request. diff --git a/modules/caddyhttp/templates/tplcontext.go b/modules/caddyhttp/templates/tplcontext.go index 814d06f..7bc0ce7 100644 --- a/modules/caddyhttp/templates/tplcontext.go +++ b/modules/caddyhttp/templates/tplcontext.go @@ -29,6 +29,7 @@ import ( "github.com/Masterminds/sprig/v3" "github.com/alecthomas/chroma/formatters/html" + "github.com/caddyserver/caddy/v2" "github.com/caddyserver/caddy/v2/modules/caddyhttp" "github.com/yuin/goldmark" highlighting "github.com/yuin/goldmark-highlighting" @@ -152,6 +153,7 @@ func (c templateContext) executeTemplateInBuffer(tplName string, buf *bytes.Buff "splitFrontMatter": c.funcSplitFrontMatter, "listFiles": c.funcListFiles, "env": c.funcEnv, + "placeholder": c.placeholder, }) parsedTpl, err := tpl.Parse(buf.String()) @@ -164,6 +166,12 @@ func (c templateContext) executeTemplateInBuffer(tplName string, buf *bytes.Buff return parsedTpl.Execute(buf, c) } +func (c templateContext) placeholder(name string) string { + repl := c.Req.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer) + value, _ := repl.GetString(name) + return value +} + func (templateContext) funcEnv(varName string) string { return os.Getenv(varName) } |