Add template
This commit is contained in:
43
templates/templates.go
Normal file
43
templates/templates.go
Normal file
@@ -0,0 +1,43 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user