From e5d66074ce1ed4e0fe329e3fdef66f8b6fd5dc55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Fri, 7 Sep 2018 09:05:25 +0200 Subject: tpl/strings: Add strings.FirstUpper Fixes #5174 --- tpl/strings/init.go | 7 +++++++ tpl/strings/strings.go | 10 ++++++++++ 2 files changed, 17 insertions(+) (limited to 'tpl') diff --git a/tpl/strings/init.go b/tpl/strings/init.go index 8ff4cf98f..7e638e6fc 100644 --- a/tpl/strings/init.go +++ b/tpl/strings/init.go @@ -155,6 +155,13 @@ func init() { }, ) + ns.AddMethodMapping(ctx.FirstUpper, + nil, + [][2]string{ + {`{{ "hugo rocks!" | strings.FirstUpper }}`, `Hugo rocks!`}, + }, + ) + ns.AddMethodMapping(ctx.Truncate, []string{"truncate"}, [][2]string{ diff --git a/tpl/strings/strings.go b/tpl/strings/strings.go index 1864bb9e0..9b8409ed6 100644 --- a/tpl/strings/strings.go +++ b/tpl/strings/strings.go @@ -325,6 +325,16 @@ func (ns *Namespace) Title(s interface{}) (string, error) { return ns.titleFunc(ss), nil } +// FirstUpper returns a string with the first character as upper case. +func (ns *Namespace) FirstUpper(s interface{}) (string, error) { + ss, err := cast.ToStringE(s) + if err != nil { + return "", err + } + + return helpers.FirstUpper(ss), nil +} + // ToLower returns a copy of the input s with all Unicode letters mapped to their // lower case. func (ns *Namespace) ToLower(s interface{}) (string, error) { -- cgit v1.2.3