mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-24 01:26:47 +01:00
templates: Date -> Now, add Replace and Truncate
This commit is contained in:
parent
7a69770026
commit
0b01489f7d
1 changed files with 18 additions and 3 deletions
|
@ -6,6 +6,7 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -13,7 +14,7 @@ import (
|
||||||
// This file contains the context and functions available for
|
// This file contains the context and functions available for
|
||||||
// use in the templates.
|
// use in the templates.
|
||||||
|
|
||||||
// context is the context with which templates are executed.
|
// Context is the context with which Caddy templates are executed.
|
||||||
type Context struct {
|
type Context struct {
|
||||||
Root http.FileSystem
|
Root http.FileSystem
|
||||||
Req *http.Request
|
Req *http.Request
|
||||||
|
@ -48,8 +49,8 @@ func (c Context) Include(filename string) (string, error) {
|
||||||
return buf.String(), nil
|
return buf.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Date returns the current timestamp in the specified format
|
// Now returns the current timestamp in the specified format.
|
||||||
func (c Context) Date(format string) string {
|
func (c Context) Now(format string) string {
|
||||||
return time.Now().Format(format)
|
return time.Now().Format(format)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -114,3 +115,17 @@ func (c Context) Method() string {
|
||||||
func (c Context) PathMatches(pattern string) bool {
|
func (c Context) PathMatches(pattern string) bool {
|
||||||
return Path(c.Req.URL.Path).Matches(pattern)
|
return Path(c.Req.URL.Path).Matches(pattern)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Truncate truncates the input string to the given length. If
|
||||||
|
// input is shorter than length, the entire string is returned.
|
||||||
|
func (c Context) Truncate(input string, length int) string {
|
||||||
|
if len(input) > length {
|
||||||
|
return input[:length]
|
||||||
|
}
|
||||||
|
return input
|
||||||
|
}
|
||||||
|
|
||||||
|
// Replace replaces instances of find in input with replacement.
|
||||||
|
func (c Context) Replace(input, find, replacement string) string {
|
||||||
|
return strings.Replace(input, find, replacement, -1)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue