Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
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. | |
adg
2012/02/28 04:28:14
s/;/'/
| |
17 func ExampleTemplate_func() { | |
18 // First we create a FuncMap with which to register the function. | |
adg
2012/02/28 04:28:14
Maybe say that this is where we give it the name "
| |
19 funcMap := template.FuncMap{ | |
20 "title": strings.Title, | |
21 } | |
22 | |
23 // A simple template definition to test our function. | |
24 // We print the input text several ways: | |
25 // - the original | |
26 // - title-cased | |
27 // - title-cased and then printed with %q | |
28 // - printed with %q and then title-cased. | |
29 templateText := "Input: {{printf `%q` .}};\n" | |
adg
2012/02/28 04:28:14
const templateText = `
...
` ?
| |
30 templateText += "Output 0: {{title .}}\n" | |
31 templateText += "Output 1: {{title . | printf `%q`}}\n" | |
32 templateText += "Output 2: {{printf `%q` . | title}}" | |
33 | |
34 // Create a template, add the function map, and parse the text. | |
35 tmpl, err := template.New("titleTest").Funcs(funcMap).Parse(templateText ) | |
36 if err != nil { | |
37 log.Fatalf("template parsing: %s", err) | |
38 } | |
39 | |
40 // Run the template to verify the output. | |
41 err = tmpl.Execute(os.Stdout, "the go programming language") | |
42 | |
43 // Output: | |
44 // Input: "the go programming language"; | |
45 // Output 0: The Go Programming Language | |
46 // Output 1: "The Go Programming Language" | |
47 // Output 2: "The Go Programming Language" | |
48 } | |
OLD | NEW |