summaryrefslogtreecommitdiff
path: root/optional/template_test.go
blob: 83b58218460d8828538e9e2352873e31f373024d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package optional_test

import (
	"html/template"
	"log"
	"os"

	"xengineering.eu/optional-go/optional"
)

func Example_template() {
	data := struct {
		Flag optional.Optional[bool]
	}{
		Flag: optional.Optional[bool]{
			Value:  false,
			Exists: false,
		},
	}

	tmpl := `Flag is: {{if .Flag.Exists}}{{.Flag.Value}}{{else}}[none]{{end}}`

	t, err := template.New("optional").Parse(tmpl)
	if err != nil {
		log.Fatal(err)
	}

	err = t.Execute(os.Stdout, data)
	if err != nil {
		log.Fatal(err)
	}

	// Output: Flag is: [none]
}