MCPcopy Create free account
hub / github.com/canopyclimate/golive / Example

Function Example

htmltmpl/example_test.go:15–83  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

13)
14
15func Example() {
16 const tpl = `
17<!DOCTYPE html>
18<html>
19 <head>
20 <meta charset="UTF-8">
21 <title>{{.Title}}</title>
22 </head>
23 <body>
24 {{range .Items}}<div>{{ . }}</div>{{else}}<div><strong>no rows</strong></div>{{end}}
25 </body>
26</html>`
27
28 check := func(err error) {
29 if err != nil {
30 log.Fatal(err)
31 }
32 }
33 t, err := template.New("webpage").Parse(tpl)
34 check(err)
35
36 data := struct {
37 Title string
38 Items []string
39 }{
40 Title: "My page",
41 Items: []string{
42 "My photos",
43 "My blog",
44 },
45 }
46
47 err = t.Execute(os.Stdout, data)
48 check(err)
49
50 noItems := struct {
51 Title string
52 Items []string
53 }{
54 Title: "My another page",
55 Items: []string{},
56 }
57
58 err = t.Execute(os.Stdout, noItems)
59 check(err)
60
61 // Output:
62 // <!DOCTYPE html>
63 // <html>
64 // <head>
65 // <meta charset="UTF-8">
66 // <title>My page</title>
67 // </head>
68 // <body>
69 // <div>My photos</div><div>My blog</div>
70 // </body>
71 // </html>
72 // <!DOCTYPE html>

Callers

nothing calls this directly

Calls 3

ParseMethod · 0.45
NewMethod · 0.45
ExecuteMethod · 0.45

Tested by

no test coverage detected