Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
a2772a59
Commit
a2772a59
authored
Mar 10, 2010
by
Andrew Gerrand
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
template: add ParseFile, MustParseFile, and associated tests
R=r CC=golang-dev
https://golang.org/cl/391041
parent
2edb02b4
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
1 deletion
+37
-1
template.go
src/pkg/template/template.go
+24
-0
template_test.go
src/pkg/template/template_test.go
+13
-1
No files found.
src/pkg/template/template.go
View file @
a2772a59
...
...
@@ -66,6 +66,7 @@ import (
"container/vector"
"fmt"
"io"
"io/ioutil"
"os"
"reflect"
"runtime"
...
...
@@ -965,6 +966,19 @@ func Parse(s string, fmap FormatterMap) (t *Template, err os.Error) {
return
}
// ParseFile is a wrapper function that creates a Template with default
// parameters (such as {} for // metacharacters). The filename identfies
// a file containing the template text, while the formatter map fmap, which
// may be nil, defines auxiliary functions for formatting variables.
// The template is returned. If any errors occur, err will be non-nil.
func
ParseFile
(
filename
string
,
fmap
FormatterMap
)
(
t
*
Template
,
err
os
.
Error
)
{
b
,
err
:=
ioutil
.
ReadFile
(
filename
)
if
err
!=
nil
{
return
nil
,
err
}
return
Parse
(
string
(
b
),
fmap
)
}
// MustParse is like Parse but panics if the template cannot be parsed.
func
MustParse
(
s
string
,
fmap
FormatterMap
)
*
Template
{
t
,
err
:=
Parse
(
s
,
fmap
)
...
...
@@ -973,3 +987,13 @@ func MustParse(s string, fmap FormatterMap) *Template {
}
return
t
}
// MustParseFile is like ParseFile but panics if the file cannot be read
// or the template cannot be parsed.
func
MustParseFile
(
filename
string
,
fmap
FormatterMap
)
*
Template
{
b
,
err
:=
ioutil
.
ReadFile
(
filename
)
if
err
!=
nil
{
panic
(
"template parse error: "
,
err
.
String
())
}
return
MustParse
(
string
(
b
),
fmap
)
}
src/pkg/template/template_test.go
View file @
a2772a59
...
...
@@ -9,7 +9,9 @@ import (
"container/vector"
"fmt"
"io"
"io/ioutil"
"json"
"os"
"testing"
)
...
...
@@ -386,6 +388,16 @@ var tests = []*Test{
}
func
TestAll
(
t
*
testing
.
T
)
{
// Parse
testAll
(
t
,
func
(
test
*
Test
)
(
*
Template
,
os
.
Error
)
{
return
Parse
(
test
.
in
,
formatters
)
})
// ParseFile
testAll
(
t
,
func
(
test
*
Test
)
(
*
Template
,
os
.
Error
)
{
ioutil
.
WriteFile
(
"_test/test.tmpl"
,
[]
byte
(
test
.
in
),
0600
)
return
ParseFile
(
"_test/test.tmpl"
,
formatters
)
})
}
func
testAll
(
t
*
testing
.
T
,
parseFunc
func
(
*
Test
)
(
*
Template
,
os
.
Error
))
{
s
:=
new
(
S
)
// initialized by hand for clarity.
s
.
header
=
"Header"
...
...
@@ -415,7 +427,7 @@ func TestAll(t *testing.T) {
var
buf
bytes
.
Buffer
for
_
,
test
:=
range
tests
{
buf
.
Reset
()
tmpl
,
err
:=
Parse
(
test
.
in
,
formatters
)
tmpl
,
err
:=
parseFunc
(
test
)
if
err
!=
nil
{
t
.
Error
(
"unexpected parse error:"
,
err
)
continue
...
...
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