shithub: hugo

Download patch

ref: 871e811339b660232557dca10a6d181a5320d745
parent: 32d15d91fc8df7fa9f1f53d22c086c7ea43da9f0
author: Tom Helmer Hansen <[email protected]>
date: Fri Jan 16 16:18:19 EST 2015

Add trim and replace template functions

--- a/docs/content/templates/functions.md
+++ b/docs/content/templates/functions.md
@@ -292,6 +292,16 @@
 
 e.g., `{{chomp "<p>Blockhead</p>\n"` → `"<p>Blockhead</p>"`
 
+### 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).
 
--- a/tpl/template.go
+++ b/tpl/template.go
@@ -1237,6 +1237,8 @@
 		"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]+$")