|
| 1 | +//go:build go1.16 |
| 2 | +// +build go1.16 |
| 3 | + |
| 4 | +// Copyright 2011 The Go Authors. All rights reserved. |
| 5 | +// Use of this source code is governed by a BSD-style |
| 6 | +// license that can be found in the LICENSE file. |
| 7 | +package template_smoke_test |
| 8 | + |
| 9 | +import ( |
| 10 | + "os" |
| 11 | + "strings" |
| 12 | + "testing" |
| 13 | + "text/template" |
| 14 | +) |
| 15 | + |
| 16 | +func TestExampleTemplate(tt *testing.T) { |
| 17 | + // Define a template. |
| 18 | + const letter = ` |
| 19 | +Dear {{.Name}}, |
| 20 | +{{if .Attended}} |
| 21 | +It was a pleasure to see you at the wedding. |
| 22 | +{{- else}} |
| 23 | +It is a shame you couldn't make it to the wedding. |
| 24 | +{{- end}} |
| 25 | +{{with .Gift -}} |
| 26 | +Thank you for the lovely {{.}}. |
| 27 | +{{end}} |
| 28 | +Best wishes, |
| 29 | +Josie |
| 30 | +` |
| 31 | + |
| 32 | + // Prepare some data to insert into the template. |
| 33 | + type Recipient struct { |
| 34 | + Name, Gift string |
| 35 | + Attended bool |
| 36 | + } |
| 37 | + var recipients = []Recipient{ |
| 38 | + {"Aunt Mildred", "bone china tea set", true}, |
| 39 | + {"Uncle John", "moleskin pants", false}, |
| 40 | + {"Cousin Rodney", "", false}, |
| 41 | + } |
| 42 | + |
| 43 | + // Create a new template and parse the letter into it. |
| 44 | + t := template.Must(template.New("letter").Parse(letter)) |
| 45 | + |
| 46 | + // Execute the template for each recipient. |
| 47 | + for _, r := range recipients { |
| 48 | + err := t.Execute(os.Stdout, r) |
| 49 | + if err != nil { |
| 50 | + tt.Log("executing template:", err) |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + // Output: |
| 55 | + // Dear Aunt Mildred, |
| 56 | + // |
| 57 | + // It was a pleasure to see you at the wedding. |
| 58 | + // Thank you for the lovely bone china tea set. |
| 59 | + // |
| 60 | + // Best wishes, |
| 61 | + // Josie |
| 62 | + // |
| 63 | + // Dear Uncle John, |
| 64 | + // |
| 65 | + // It is a shame you couldn't make it to the wedding. |
| 66 | + // Thank you for the lovely moleskin pants. |
| 67 | + // |
| 68 | + // Best wishes, |
| 69 | + // Josie |
| 70 | + // |
| 71 | + // Dear Cousin Rodney, |
| 72 | + // |
| 73 | + // It is a shame you couldn't make it to the wedding. |
| 74 | + // |
| 75 | + // Best wishes, |
| 76 | + // Josie |
| 77 | +} |
| 78 | + |
| 79 | +// The following example is duplicated in html/template; keep them in sync. |
| 80 | + |
| 81 | +func TestExampleTemplate_block(tt *testing.T) { |
| 82 | + const ( |
| 83 | + master = `Names:{{block "list" .}}{{"\n"}}{{range .}}{{println "-" .}}{{end}}{{end}}` |
| 84 | + overlay = `{{define "list"}} {{join . ", "}}{{end}} ` |
| 85 | + ) |
| 86 | + var ( |
| 87 | + funcs = template.FuncMap{"join": strings.Join} |
| 88 | + guardians = []string{"Gamora", "Groot", "Nebula", "Rocket", "Star-Lord"} |
| 89 | + ) |
| 90 | + masterTmpl, err := template.New("master").Funcs(funcs).Parse(master) |
| 91 | + if err != nil { |
| 92 | + tt.Fatal(err) |
| 93 | + } |
| 94 | + overlayTmpl, err := template.Must(masterTmpl.Clone()).Parse(overlay) |
| 95 | + if err != nil { |
| 96 | + tt.Fatal(err) |
| 97 | + } |
| 98 | + if err := masterTmpl.Execute(os.Stdout, guardians); err != nil { |
| 99 | + tt.Fatal(err) |
| 100 | + } |
| 101 | + if err := overlayTmpl.Execute(os.Stdout, guardians); err != nil { |
| 102 | + tt.Fatal(err) |
| 103 | + } |
| 104 | + // Output: |
| 105 | + // Names: |
| 106 | + // - Gamora |
| 107 | + // - Groot |
| 108 | + // - Nebula |
| 109 | + // - Rocket |
| 110 | + // - Star-Lord |
| 111 | + // Names: Gamora, Groot, Nebula, Rocket, Star-Lord |
| 112 | +} |
0 commit comments