summaryrefslogtreecommitdiff
path: root/optional/template_test.go
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2024-11-08 17:00:09 +0100
committerxengineering <me@xengineering.eu>2024-11-08 17:57:32 +0100
commit45af2e59bbbf9e08014bda2d894b402f574c9516 (patch)
tree04c2163641493c4f96e506a6fc599f4f14d6086d /optional/template_test.go
parente3a3949959f3223ec69f2c60d4e620f8653dc2ff (diff)
downloadoptional-go-45af2e59bbbf9e08014bda2d894b402f574c9516.tar
optional-go-45af2e59bbbf9e08014bda2d894b402f574c9516.tar.zst
optional-go-45af2e59bbbf9e08014bda2d894b402f574c9516.zip
Add example for usage in Go templates
Diffstat (limited to 'optional/template_test.go')
-rw-r--r--optional/template_test.go34
1 files changed, 34 insertions, 0 deletions
diff --git a/optional/template_test.go b/optional/template_test.go
new file mode 100644
index 0000000..ab4ce76
--- /dev/null
+++ b/optional/template_test.go
@@ -0,0 +1,34 @@
+package optional_test
+
+import (
+ "html/template"
+ "log"
+ "os"
+
+ "xengineering.eu/optional-go/optional"
+)
+
+func ExampleTemplate() {
+ 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]
+}