Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(350)

Side by Side Diff: src/pkg/text/template/examplefunc_test.go

Issue 5694100: code review 5694100: text/template: add example showing use of custom function (Closed)
Patch Set: diff -r 9fa2b8dc359b https://code.google.com/p/go/ Created 13 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b