Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "html/template"
- "net/http"
- )
- type person struct {
- Name string
- Age int
- Admin bool
- }
- var tpl *template.Template
- func init() {
- tpl = template.Must(template.ParseGlob("templates/*.gohtml"))
- }
- func main() {
- http.HandleFunc("/", index)
- http.ListenAndServe(":8080", nil)
- }
- func index(res http.ResponseWriter, req *http.Request) {
- p1 := person{
- Name: "Janis",
- Age: 24,
- Admin: false,
- }
- p2 := person{
- Name: "",
- Age: 27,
- Admin: true,
- }
- p3 := person{
- Name: "Edgars",
- Age: 23,
- Admin: false,
- }
- persons := []person{p1, p2, p3}
- tpl.ExecuteTemplate(res, "two.gohtml", persons)
- }
- /*
- ____one.gohtml
- {{define "polar"}}
- text from one.gohtml
- {{end}}
- ____two.gothml
- <body>
- This might be two.gohtml? But I am not sure.
- <h3>
- {{range .}}
- {{.}}
- {{end}}
- <br />
- {{range .}}
- {{if .Name}}
- {{.Name}}
- {{end}}
- {{end}}
- <br />
- {{range .}}
- {{if and .Name .Age}}
- {{.Name}} --- {{.Age}} --- {{.Admin}} ,
- {{end}}
- {{end}}
- </h3>
- {{template "polar"}}
- </body>
- */
Advertisement
Add Comment
Please, Sign In to add comment