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

Side by Side Diff: src/pkg/exp/template/helper.go

Issue 4823056: code review 4823056: exp/template: make Set.ParseFile etc resolve functions ... (Closed)
Patch Set: diff -r 1d598e5573e5 https://go.googlecode.com/hg/ Created 13 years, 7 months 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
1 // Copyright 2011 The Go Authors. All rights reserved. 1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 // Helper functions to make constructing templates and sets easier. 5 // Helper functions to make constructing templates and sets easier.
6 6
7 package template 7 package template
8 8
9 import ( 9 import (
10 "fmt" 10 "fmt"
(...skipping 17 matching lines...) Expand all
28 // ParseFile reads the template definition from a file and parses it to 28 // ParseFile reads the template definition from a file and parses it to
29 // construct an internal representation of the template for execution. 29 // construct an internal representation of the template for execution.
30 func (t *Template) ParseFile(filename string) os.Error { 30 func (t *Template) ParseFile(filename string) os.Error {
31 b, err := ioutil.ReadFile(filename) 31 b, err := ioutil.ReadFile(filename)
32 if err != nil { 32 if err != nil {
33 return err 33 return err
34 } 34 }
35 return t.Parse(string(b)) 35 return t.Parse(string(b))
36 } 36 }
37 37
38 // ParseFileInSet is the same as ParseFile except that function bindings
39 // are checked against those in the set and the template is added
40 // to the set.
41 func (t *Template) ParseFileInSet(filename string, set *Set) os.Error {
42 b, err := ioutil.ReadFile(filename)
43 if err != nil {
44 return err
45 }
46 return t.ParseInSet(string(b), set)
47 }
48
38 // MustParseFile reads the template definition from a file and parses it to 49 // MustParseFile reads the template definition from a file and parses it to
39 // construct an internal representation of the template for execution. 50 // construct an internal representation of the template for execution.
40 // It panics if the file cannot be read or the template cannot be parsed. 51 // It panics if the file cannot be read or the template cannot be parsed.
41 func (t *Template) MustParseFile(filename string) *Template { 52 func (t *Template) MustParseFile(filename string) *Template {
42 if err := t.ParseFile(filename); err != nil { 53 if err := t.ParseFile(filename); err != nil {
43 panic(err) 54 panic(err)
44 } 55 }
45 return t 56 return t
46 } 57 }
47 58
48 // ParseFile creates a new Template and parses the template definition from 59 // ParseFile creates a new Template and parses the template definition from
49 // the named file. The template name is the base name of the file. 60 // the named file. The template name is the base name of the file.
50 func ParseFile(filename string) (*Template, os.Error) { 61 func ParseFile(filename string) (*Template, os.Error) {
51 t := New(filepath.Base(filename)) 62 t := New(filepath.Base(filename))
52 return t, t.ParseFile(filename) 63 return t, t.ParseFile(filename)
53 } 64 }
54 65
66 // ParseFileInSet creates a new Template and parses the template
67 // definition from the named file. The template name is the base name
68 // of the file. It also adds the template to the set. Function bindings are
69 //checked against those in the set.
70 func ParseFileInSet(filename string, set *Set) (*Template, os.Error) {
71 t := New(filepath.Base(filename))
72 return t, t.ParseFileInSet(filename, set)
73 }
74
55 // MustParseFile creates a new Template and parses the template definition 75 // MustParseFile creates a new Template and parses the template definition
56 // from the named file. The template name is the base name of the file. 76 // from the named file. The template name is the base name of the file.
57 // It panics if the file cannot be read or the template cannot be parsed. 77 // It panics if the file cannot be read or the template cannot be parsed.
58 func MustParseFile(filename string) *Template { 78 func MustParseFile(filename string) *Template {
59 return New(filepath.Base(filename)).MustParseFile(filename) 79 return New(filepath.Base(filename)).MustParseFile(filename)
60 } 80 }
61 81
62 // Functions and methods to parse a set. 82 // Functions and methods to parse a set.
63 83
64 // MustParse parses a string into a set of named templates. 84 // MustParse parses a string into a set of named templates.
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 // its file. 192 // its file.
173 // Unlike with ParseFile, each file should be a stand-alone template 193 // Unlike with ParseFile, each file should be a stand-alone template
174 // definition suitable for Template.Parse (not Set.Parse); that is, the 194 // definition suitable for Template.Parse (not Set.Parse); that is, the
175 // file does not contain {{define}} clauses. ParseTemplateFile is 195 // file does not contain {{define}} clauses. ParseTemplateFile is
176 // therefore equivalent to calling the ParseFile function to create 196 // therefore equivalent to calling the ParseFile function to create
177 // individual templates, which are then added to the set. 197 // individual templates, which are then added to the set.
178 // Each file must be parseable by itself. Parsing stops if an error is 198 // Each file must be parseable by itself. Parsing stops if an error is
179 // encountered. 199 // encountered.
180 func (s *Set) ParseTemplateFile(filenames ...string) os.Error { 200 func (s *Set) ParseTemplateFile(filenames ...string) os.Error {
181 for _, filename := range filenames { 201 for _, filename := range filenames {
182 » » t, err := ParseFile(filename) 202 » » _, err := ParseFileInSet(filename, s)
183 if err != nil { 203 if err != nil {
184 return err 204 return err
185 } 205 }
186 if err := s.add(t); err != nil {
187 return err
188 }
189 } 206 }
190 return nil 207 return nil
191 } 208 }
192 209
193 // MustParseTemplateFile is like ParseTemplateFile but 210 // MustParseTemplateFile is like ParseTemplateFile but
194 // panics if there is an error. 211 // panics if there is an error.
195 func (s *Set) MustParseTemplateFile(filenames ...string) *Set { 212 func (s *Set) MustParseTemplateFile(filenames ...string) *Set {
196 err := s.ParseTemplateFile(filenames...) 213 err := s.ParseTemplateFile(filenames...)
197 if err != nil { 214 if err != nil {
198 panic(err) 215 panic(err)
(...skipping 10 matching lines...) Expand all
209 // therefore equivalent to calling the ParseFile function to create 226 // therefore equivalent to calling the ParseFile function to create
210 // individual templates, which are then added to the set. 227 // individual templates, which are then added to the set.
211 // Each file must be parseable by itself. Parsing stops if an error is 228 // Each file must be parseable by itself. Parsing stops if an error is
212 // encountered. 229 // encountered.
213 func (s *Set) ParseTemplateFiles(pattern string) os.Error { 230 func (s *Set) ParseTemplateFiles(pattern string) os.Error {
214 filenames, err := filepath.Glob(pattern) 231 filenames, err := filepath.Glob(pattern)
215 if err != nil { 232 if err != nil {
216 return err 233 return err
217 } 234 }
218 for _, filename := range filenames { 235 for _, filename := range filenames {
219 » » t, err := ParseFile(filename) 236 » » _, err := ParseFileInSet(filename, s)
220 if err != nil { 237 if err != nil {
221 return err 238 return err
222 } 239 }
223 if err := s.add(t); err != nil {
224 return err
225 }
226 } 240 }
227 return nil 241 return nil
228 } 242 }
229 243
230 // MustParseTemplateFile is like ParseTemplateFiles but 244 // MustParseTemplateFile is like ParseTemplateFiles but
231 // panics if there is an error. 245 // panics if there is an error.
232 func (s *Set) MustParseTemplateFiles(pattern string) *Set { 246 func (s *Set) MustParseTemplateFiles(pattern string) *Set {
233 err := s.ParseTemplateFiles(pattern) 247 err := s.ParseTemplateFiles(pattern)
234 if err != nil { 248 if err != nil {
235 panic(err) 249 panic(err)
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
302 // MustParseTemplateFiles is like ParseTemplateFiles but 316 // MustParseTemplateFiles is like ParseTemplateFiles but
303 // panics if there is a parse error or other problem 317 // panics if there is a parse error or other problem
304 // constructing the set. 318 // constructing the set.
305 func MustParseTemplateFiles(pattern string) *Set { 319 func MustParseTemplateFiles(pattern string) *Set {
306 set, err := ParseTemplateFiles(pattern) 320 set, err := ParseTemplateFiles(pattern)
307 if err != nil { 321 if err != nil {
308 panic(err) 322 panic(err)
309 } 323 }
310 return set 324 return set
311 } 325 }
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