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