Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
68b35b08
Commit
68b35b08
authored
Feb 19, 2012
by
Rob Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
templates: minor edits to the documentation
R=golang-dev, rsc CC=golang-dev
https://golang.org/cl/5677084
parent
253c1392
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
27 additions
and
3 deletions
+27
-3
src/pkg/text/template/doc.go
src/pkg/text/template/doc.go
+23
-1
src/pkg/text/template/parse/parse.go
src/pkg/text/template/parse/parse.go
+4
-2
No files found.
src/pkg/text/template/doc.go
View file @
68b35b08
...
...
@@ -22,6 +22,20 @@ Actions may not span newlines, although comments can.
Once constructed, a template may be executed safely in parallel.
Here is a trivial example that prints "17 items are made of wool".
type Inventory struct {
Material string
Count uint
}
sweaters := Inventory{"wool", 17}
tmpl, err := template.New("test").Parse("{{.Count}} items are made of {{.Material}}")
if err != nil { panic(err) }
err = tmpl.Execute(os.Stdout, sweaters)
if err != nil { panic(err) }
More intricate examples appear below.
Actions
Here is the list of actions. "Arguments" and "pipelines" are evaluations of
...
...
@@ -128,6 +142,11 @@ An argument is a simple value, denoted by one of the following.
.Field1.Key1.Method1.Field2.Key2.Method2
Methods can also be evaluated on variables, including chaining:
$x.Method1.Field
- The name of a niladic function-valued struct field of the data,
preceded by a period, such as
.Function
Function-valued fields behave like methods (of structs) but do not
pass a receiver.
- The name of a niladic function, such as
fun
The result is the value of invoking the function, fun(). The return
...
...
@@ -148,6 +167,9 @@ value (argument) or a function or method call, possibly with multiple arguments:
The result is the value of calling the method with the
arguments:
dot.Method(Argument1, etc.)
.Function [Argument...]
A function-valued field of a struct works like a method but does
not pass the receiver.
functionName [Argument...]
The result is the value of calling the function associated
with the name:
...
...
@@ -303,7 +325,7 @@ produce the text
By construction, a template may reside in only one association. If it's
necessary to have a template addressable from multiple associations, the
template definition must be parsed multiple times to create distinct *Template
values.
values
, or must be copied with the Clone or AddParseTree method
.
Parse may be called multiple times to assemble the various associated templates;
see the ParseFiles and ParseGlob functions and methods for simple ways to parse
...
...
src/pkg/text/template/parse/parse.go
View file @
68b35b08
...
...
@@ -2,8 +2,10 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package parse builds parse trees for templates. The grammar is defined
// in the documents for the template package.
// Package parse builds parse trees for templates as defined by text/template
// and html/template. Clients should use those packages to construct templates
// rather than this one, which provides shared internal data structures not
// intended for general use.
package
parse
import
(
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment