Skip to content

Commit

Permalink
tpl/urls: Return empty string when JoinPath has zero args
Browse files Browse the repository at this point in the history
  • Loading branch information
jmooring authored and bep committed May 20, 2023
1 parent 065ae00 commit 150d190
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
3 changes: 2 additions & 1 deletion docs/content/en/functions/urls.JoinPath.md
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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"
Expand Down
11 changes: 7 additions & 4 deletions tpl/urls/urls.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 150d190

Please sign in to comment.