From 871e811339b660232557dca10a6d181a5320d745 Mon Sep 17 00:00:00 2001 From: Tom Helmer Hansen Date: Fri, 16 Jan 2015 21:18:19 +0100 Subject: [PATCH] Add trim and replace template functions --- docs/content/templates/functions.md | 10 ++++++++++ tpl/template.go | 2 ++ 2 files changed, 12 insertions(+) diff --git a/docs/content/templates/functions.md b/docs/content/templates/functions.md index fcc0f45ac..1b759e066 100644 --- a/docs/content/templates/functions.md +++ b/docs/content/templates/functions.md @@ -292,6 +292,16 @@ Removes any trailing newline characters. Useful in a pipeline to remove newlines e.g., `{{chomp "

Blockhead

\n"` → `"

Blockhead

"` +### trim +Trim returns a slice of the string with all leading and trailing characters contained in cutset removed. + +e.g. `{{ trim "++Batman--" "+-" }}` → "Batman" + +### replace +Replace all occurences of the search string with the replacement string. + +e.g. `{{ replace "Batman and Robin" "Robin" "Catwoman" }}` → "Batman and Catwoman" + ### highlight Take a string of code and a language, uses Pygments to return the syntax highlighted code in HTML. Used in the [highlight shortcode](/extras/highlighting). diff --git a/tpl/template.go b/tpl/template.go index a051eba0b..f392f81f0 100644 --- a/tpl/template.go +++ b/tpl/template.go @@ -1237,6 +1237,8 @@ func init() { "relref": RelRef, "apply": Apply, "chomp": Chomp, + "replace": func(a string, b string, c string) string { return strings.Replace(a, b, c, -1) }, + "trim": func(a string, b string) string { return strings.Trim(a, b) }, } chompRegexp = regexp.MustCompile("[\r\n]+$")