From 150d190ff041f7da905afea7f3b9ca6bd4a31a48 Mon Sep 17 00:00:00 2001 From: Joe Mooring Date: Fri, 19 May 2023 08:29:30 -0700 Subject: tpl/urls: Return empty string when JoinPath has zero args --- docs/content/en/functions/urls.JoinPath.md | 3 ++- tpl/urls/urls.go | 11 +++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/docs/content/en/functions/urls.JoinPath.md b/docs/content/en/functions/urls.JoinPath.md index c0e0464b6..f11632367 100644 --- a/docs/content/en/functions/urls.JoinPath.md +++ b/docs/content/en/functions/urls.JoinPath.md @@ -1,6 +1,6 @@ --- title: urls.JoinPath -description: Joins the provided elements into a URL string and cleans the result of any ./ or ../ elements. +description: Joins the provided elements into a URL string and cleans the result of any ./ or ../ elements. If the argument list is empty, JoinPath returns an empty string. categories: [functions] menu: docs: @@ -10,6 +10,7 @@ signature: ["urls.JoinPath ELEMENT..."] --- ```go-html-template +{{ urls.JoinPath }} → "" {{ urls.JoinPath "" }} → "/" {{ urls.JoinPath "a" }} → "a" {{ urls.JoinPath "a" "b" }} → "a/b" diff --git a/tpl/urls/urls.go b/tpl/urls/urls.go index 5de2342d4..6ae447ca2 100644 --- a/tpl/urls/urls.go +++ b/tpl/urls/urls.go @@ -187,16 +187,19 @@ func (ns *Namespace) AbsLangURL(s any) (template.HTML, error) { } // JoinPath joins the provided elements into a URL string and cleans the result -// of any ./ or ../ elements. +// of any ./ or ../ elements. If the argument list is empty, JoinPath returns +// an empty string. func (ns *Namespace) JoinPath(elements ...any) (string, error) { + if len(elements) == 0 { + return "", nil + } + var selements []string for _, e := range elements { switch v := e.(type) { case []string: - for _, e := range v { - selements = append(selements, e) - } + selements = append(selements, v...) case []any: for _, e := range v { se, err := cast.ToStringE(e) -- cgit v1.2.3