Html/template optional argument in function

I want to create a function that adds my ‘site title’ to my ‘page title’, or just show the ‘site title’ if no ‘page title’ is given.

I ran into issues; wrong number of args for title: want 1 got 0

I can solve it by doing this:
<title>{{if .Title}}{{.Title | title}}{{else}}{{"" | title}}{{end}}</title>

But what I really want is:
<title>{{ .Title | title }}</title>

It works when you use this function:

func title(i ...string) string {
	if len(i) == 0 || i[0] == "" {
		return siteName
	}

	return fmt.Sprintf("%s - %s", i[0], siteName)
}

But it ain’t pretty. Is there a better solution?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.