Commit 0b01489f authored by Matthew Holt's avatar Matthew Holt

templates: Date -> Now, add Replace and Truncate

parent 7a697700
...@@ -6,6 +6,7 @@ import ( ...@@ -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 ( ...@@ -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) { ...@@ -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 { ...@@ -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)
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment