Debian Codegraph @

tpl/internal/go_templates/texttemplate/examplefunc_test.go

raw
1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build go1.13
6 // +build go1.13
7
8 package template_test
9
10 import (
11 "log"
12 "os"
13 "strings"
14 "text/template"
15 )
16
17 // This example demonstrates a custom function to process template text.
18 // It installs the strings.Title function and uses it to
19 // Make Title Text Look Good In Our Template's Output.
20 func ExampleTemplate_func() {
21 // First we create a FuncMap with which to register the function.
22 funcMap := template.FuncMap{
23 // The name "title" is what the function will be called in the template text.
24 "title": strings.Title,
25 }
26
27 // A simple template definition to test our function.
28 // We print the input text several ways:
29 // - the original
30 // - title-cased
31 // - title-cased and then printed with %q
32 // - printed with %q and then title-cased.
33 const templateText = `
34 Input: {{printf "%q" .}}
35 Output 0: {{title .}}
36 Output 1: {{title . | printf "%q"}}
37 Output 2: {{printf "%q" . | title}}
38 `
39
40 // Create a template, add the function map, and parse the text.
41 tmpl, err := template.New("titleTest").Funcs(funcMap).Parse(templateText)
42 if err != nil {
43 log.Fatalf("parsing: %s", err)
44 }
45
46 // Run the template to verify the output.
47 err = tmpl.Execute(os.Stdout, "the go programming language")
48 if err != nil {
49 log.Fatalf("execution: %s", err)
50 }
51
52 // Output:
53 // Input: "the go programming language"
54 // Output 0: The Go Programming Language
55 // Output 1: "The Go Programming Language"
56 // Output 2: "The Go Programming Language"
57 }
58
59 // This example demonstrates registering two custom template functions
60 // and how to overwite one of the functions after the template has been
61 // parsed. Overwriting can be used, for example, to alter the operation
62 // of cloned templates.
63 func ExampleTemplate_funcs() {
64
65 // Define a simple template to test the functions.
66 const tmpl = `{{ . | lower | repeat }}`
67
68 // Define the template funcMap with two functions.
69 var funcMap = template.FuncMap{
70 "lower": strings.ToLower,
71 "repeat": func(s string) string { return strings.Repeat(s, 2) },
72 }
73
74 // Define a New template, add the funcMap using Funcs and then Parse
75 // the template.
76 parsedTmpl, err := template.New("t").Funcs(funcMap).Parse(tmpl)
77 if err != nil {
78 log.Fatal(err)
79 }
80 if err := parsedTmpl.Execute(os.Stdout, "ABC\n"); err != nil {
81 log.Fatal(err)
82 }
83
84 // [Funcs] must be called before a template is parsed to add
85 // functions to the template. [Funcs] can also be used after a
86 // template is parsed to overwrite template functions.
87 //
88 // Here the function identified by "repeat" is overwritten.
89 parsedTmpl.Funcs(template.FuncMap{
90 "repeat": func(s string) string { return strings.Repeat(s, 3) },
91 })
92 if err := parsedTmpl.Execute(os.Stdout, "DEF\n"); err != nil {
93 log.Fatal(err)
94 }
95 // Output:
96 // abc
97 // abc
98 // def
99 // def
100 // def
101 }
102
103 // This example demonstrates how to use "if".
104 func ExampleTemplate_if() {
105 type book struct {
106 Stars float32
107 Name string
108 }
109
110 tpl, err := template.New("book").Parse(`{{ if (gt .Stars 4.0) }}"{{.Name }}" is a great book.{{ else }}"{{.Name}}" is not a great book.{{ end }}`)
111 if err != nil {
112 log.Fatalf("failed to parse template: %s", err)
113 }
114
115 b := &book{
116 Stars: 4.9,
117 Name: "Good Night, Gopher",
118 }
119 err = tpl.Execute(os.Stdout, b)
120 if err != nil {
121 log.Fatalf("failed to execute template: %s", err)
122 }
123
124 // Output:
125 // "Good Night, Gopher" is a great book.
126 }