diff --git a/README.md b/README.md index 6397246..4a285b2 100644 --- a/README.md +++ b/README.md @@ -251,6 +251,62 @@ Returns a plural suffix if the value is not 1. You can specify both a singular a +#### boolYesno + +Returns argument strings according to the given boolean value. + +**Argument:** strings for true and false + +``` +{{ value | boolYesno "yes!" "no!" }} +``` + + +#### stringRjust + +Right-aligns the given string in a field of a given width. This function also supports unicode strings. + +``` +{{ value | stringRjust 10 }} +``` + +**Examples** + +1. If input is {{ "Go" | stringRjust 10 }}, the output will be " Go". +1. If input is {{ "안녕하세요" | stringRjust 10 }}, the output will be " 안녕하세요". + + + +#### stringLjust + +Left-aligns the given string in a field of a given width. This function also supports unicode strings. + +``` +{{ value | stringLjust 10 }} +``` + +**Examples** + +1. If input is {{ "Go" | stringLjust 10 }}, the output will be "Go ". +1. If input is {{ "안녕하세요" | stringLjust 10 }}, the output will be "안녕하세요 ". + + + +#### stringCenter + +Centers the given string in a field of a given width. This function also supports unicode strings. + +``` +{{ value | stringCenter 10 }} +``` + +**Examples** + +1. If input is {{ "Go" | stringCenter 10 }}, the output will be " Go ". +1. If input is {{ "안녕하세요" | stringCenter 10 }}, the output will be " 안녕하세요 ". + + + ## Goal The main goal is implementing all built-in template filters of Django & Jinja2. diff --git a/gtf.go b/gtf.go index 383138e..3c02016 100644 --- a/gtf.go +++ b/gtf.go @@ -109,6 +109,50 @@ var GtfFuncMap = template.FuncMap { return bits[1] }, + "boolYesno": func(yes string, no string, value bool) string { + defer recovery() + + if value { + return yes + } + + return no + }, + "stringRjust": func(arg int, value string) string { + defer recovery() + + n := arg - len([]rune(value)) + + if n > 0 { + value = strings.Repeat(" ", n) + value + } + + return value + }, + "stringLjust": func(arg int, value string) string { + defer recovery() + + n := arg - len([]rune(value)) + + if n > 0 { + value = value + strings.Repeat(" ", n) + } + + return value + }, + "stringCenter": func(arg int, value string) string { + defer recovery() + + n := arg - len([]rune(value)) + + if n > 0 { + left := n / 2 + right := n - left + value = strings.Repeat(" ", left) + value + strings.Repeat(" ", right) + } + + return value + }, } // gtf.New is a wrapper function of template.New(http://golang.org/pkg/text/template/#New). diff --git a/gtf_test.go b/gtf_test.go index 0f63227..029dd69 100644 --- a/gtf_test.go +++ b/gtf_test.go @@ -113,4 +113,28 @@ func TestGtfFuncMap(t *testing.T) { ParseTest(&buffer, "{{ 2 | intPluralize \"y,ies,s\" }}") AssertEqual(t, &buffer, "") + + ParseTest(&buffer, "{{ true | boolYesno \"yes~\" \"no~\" }}") + AssertEqual(t, &buffer, "yes~") + + ParseTest(&buffer, "{{ false | boolYesno \"yes~\" \"no~\" }}") + AssertEqual(t, &buffer, "no~") + + ParseTest(&buffer, "{{ \"Go\" | stringRjust 10 }}") + AssertEqual(t, &buffer, " Go") + + ParseTest(&buffer, "{{ \"안녕하세요\" | stringRjust 10 }}") + AssertEqual(t, &buffer, " 안녕하세요") + + ParseTest(&buffer, "{{ \"Go\" | stringLjust 10 }}") + AssertEqual(t, &buffer, "Go ") + + ParseTest(&buffer, "{{ \"안녕하세요\" | stringLjust 10 }}") + AssertEqual(t, &buffer, "안녕하세요 ") + + ParseTest(&buffer, "{{ \"Go\" | stringCenter 10 }}") + AssertEqual(t, &buffer, " Go ") + + ParseTest(&buffer, "{{ \"안녕하세요\" | stringCenter 10 }}") + AssertEqual(t, &buffer, " 안녕하세요 ") }