44 lines
893 B
Go
44 lines
893 B
Go
|
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", `
|
||
|
<dl>
|
||
|
{{range . -}}
|
||
|
<dt>{{ .Name }}</dt>
|
||
|
<dd>
|
||
|
<p>{{ .Bio }}</p>
|
||
|
<p>{{ if .IsAdult -}} Adult {{- else -}} Teen {{- end }}</p>
|
||
|
</dd>
|
||
|
{{end}}
|
||
|
</dl>
|
||
|
`)
|
||
|
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("<body>\n%s</body>\n", b.String())
|
||
|
fmt.Println("Output as follows:")
|
||
|
fmt.Println(output)
|
||
|
}
|