diff --git a/.gitignore b/.gitignore index 7b4f01b..9495981 100644 --- a/.gitignore +++ b/.gitignore @@ -53,3 +53,4 @@ chatserver/chatserver example-httptcp/example-httptcp example-httpserver/example-httpserver http-router/http-router +tpl-text/tpl-text diff --git a/tpl-text/main.go b/tpl-text/main.go new file mode 100644 index 0000000..9a411fe --- /dev/null +++ b/tpl-text/main.go @@ -0,0 +1,51 @@ +package main + +import ( + "log" + "os" + //"text/template" + "html/template" +) + +type Data struct { + MyTitle string + Lst []int + Thing interface{} +} + +func main() { + var err error + + //tpl, err := template.ParseFiles("tpl.html") + tpl := template.New("tpl.html") + tpl = tpl.Funcs(template.FuncMap{ + "myFunc": func(s string) string { + return s + s + }, + }) + + tpl, err = tpl.ParseFiles("tpl.html") + if err != nil { + log.Fatalln(err) + } + /* + data := map[string]string{ + "myTitle": "plop", + } + data := []int{1, 2, 3} + data := map[string]interface{}{ + "myTitle": "plop", + "lst": []int{1, 2, 3}, + } + */ + data := Data{ + //"plop", + "", + []int{1, 2, 3}, + template.HTML(""), + } + err = tpl.Execute(os.Stdout, data) + if err != nil { + log.Fatalln(err) + } +} diff --git a/tpl-text/tpl.html b/tpl-text/tpl.html new file mode 100644 index 0000000..562dbb2 --- /dev/null +++ b/tpl-text/tpl.html @@ -0,0 +1,18 @@ + + +
+{{ .Thing }}
+ +