package templates
import (
"fmt"
"os"
"strings"
"text/template"
)
func CreateTemplate(label, content string) *template.Template {
return template.Must(template.New(label).Parse(content))
}
func Templates() {
t1 := CreateTemplate("bioList", `
{{range . -}}
- {{ .Name }}
-
{{ .Bio }}
{{ if .IsAdult -}} Adult {{- else -}} Teen {{- end }}
{{end}}
`)
peeps := []struct {
Name, Bio string
Age uint
IsAdult bool
}{
{"Paul Atreides", "Duke of Arrakis, Lisan Al Gaib", 24, true},
{"Leto Atreides", "Former Duke of Arrakis and father of Paul Atreides", 43, true},
{"Hidenori", "Unrelated character from DLoHB", 16, false},
}
t1.Execute(os.Stdout, peeps)
var b strings.Builder
t1.Execute(&b, peeps)
output := fmt.Sprintf("\n%s\n", b.String())
fmt.Println("Output as follows:")
fmt.Println(output)
}