summaryrefslogtreecommitdiff
path: root/modules/caddyhttp/templates/templates.go
diff options
context:
space:
mode:
authorAleks <git001@users.noreply.github.com>2022-05-25 01:47:08 +0200
committerGitHub <noreply@github.com>2022-05-24 19:47:08 -0400
commit6891f7f421eac71dac8f8687255ede5189e7eb3a (patch)
tree7974c4b7577eb9982956befff02d190c47dd03eb /modules/caddyhttp/templates/templates.go
parent499ad6d182d6993b1d149a5ee96dbd37d14cacd7 (diff)
templates: Add `humanize` function (#4767)
Co-authored-by: Francis Lavoie <lavofr@gmail.com>
Diffstat (limited to 'modules/caddyhttp/templates/templates.go')
-rw-r--r--modules/caddyhttp/templates/templates.go23
1 files changed, 23 insertions, 0 deletions
diff --git a/modules/caddyhttp/templates/templates.go b/modules/caddyhttp/templates/templates.go
index b2fe184..85612ba 100644
--- a/modules/caddyhttp/templates/templates.go
+++ b/modules/caddyhttp/templates/templates.go
@@ -238,6 +238,29 @@ func init() {
// {{stripHTML "Shows <b>only</b> text content"}}
// ```
//
+// ##### `humanize`
+//
+// Transforms size and time inputs to a human readable format.
+// This uses the [go-humanize](https://github.com/dustin/go-humanize) library.
+//
+// The first argument must be a format type, and the last argument
+// is the input, or the input can be piped in. The supported format
+// types are:
+// - **size** which turns an integer amount of bytes into a string like `2.3 MB`
+// - **time** which turns a time string into a relative time string like `2 weeks ago`
+//
+// For the `time` format, the layout for parsing the input can be configured
+// by appending a colon `:` followed by the desired time layout. You can
+// find the documentation on time layouts [in Go's docs](https://pkg.go.dev/time#pkg-constants).
+// The default time layout is `RFC1123Z`, i.e. `Mon, 02 Jan 2006 15:04:05 -0700`.
+//
+// ```
+// {{humanize "size" "2048000"}}
+// {{placeholder "http.response.header.Content-Length" | humanize "size"}}
+// {{humanize "time" "Fri, 05 May 2022 15:04:05 +0200"}}
+// {{humanize "time:2006-Jan-02" "2022-May-05"}}
+// ```
+
type Templates struct {
// The root path from which to load files. Required if template functions
// accessing the file system are used (such as include). Default is