tpl/urls: Return empty string when JoinPath has zero args

This commit is contained in:
Joe Mooring 2023-05-19 08:29:30 -07:00 committed by Bjørn Erik Pedersen
parent 065ae003a5
commit 150d190ff0
2 changed files with 9 additions and 5 deletions

View file

@ -1,6 +1,6 @@
--- ---
title: urls.JoinPath 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] categories: [functions]
menu: menu:
docs: docs:
@ -10,6 +10,7 @@ signature: ["urls.JoinPath ELEMENT..."]
--- ---
```go-html-template ```go-html-template
{{ urls.JoinPath }} → ""
{{ urls.JoinPath "" }} → "/" {{ urls.JoinPath "" }} → "/"
{{ urls.JoinPath "a" }} → "a" {{ urls.JoinPath "a" }} → "a"
{{ urls.JoinPath "a" "b" }} → "a/b" {{ urls.JoinPath "a" "b" }} → "a/b"

View file

@ -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 // 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) { func (ns *Namespace) JoinPath(elements ...any) (string, error) {
if len(elements) == 0 {
return "", nil
}
var selements []string var selements []string
for _, e := range elements { for _, e := range elements {
switch v := e.(type) { switch v := e.(type) {
case []string: case []string:
for _, e := range v { selements = append(selements, v...)
selements = append(selements, e)
}
case []any: case []any:
for _, e := range v { for _, e := range v {
se, err := cast.ToStringE(e) se, err := cast.ToStringE(e)