@@ -2,6 +2,7 @@ package mustache
22
33import (
44 "bytes"
5+ "errors"
56 "fmt"
67 "io"
78 "io/ioutil"
@@ -42,7 +43,7 @@ type parseError struct {
4243 message string
4344}
4445
45- func (p parseError ) String () string { return fmt .Sprintf ("line %d: %s" , p .line , p .message ) }
46+ func (p parseError ) Error () string { return fmt .Sprintf ("line %d: %s" , p .line , p .message ) }
4647
4748var (
4849 esc_quot = []byte (""" )
@@ -78,13 +79,13 @@ func htmlEscape(w io.Writer, s []byte) {
7879 w .Write (s [last :])
7980}
8081
81- func (tmpl * Template ) readString (s string ) (string , os. Error ) {
82+ func (tmpl * Template ) readString (s string ) (string , error ) {
8283 i := tmpl .p
8384 newlines := 0
8485 for true {
8586 //are we at the end of the string?
8687 if i + len (s ) > len (tmpl .data ) {
87- return tmpl .data [tmpl .p :], os .EOF
88+ return tmpl .data [tmpl .p :], io .EOF
8889 }
8990
9091 if tmpl .data [i ] == '\n' {
@@ -120,7 +121,7 @@ func (tmpl *Template) readString(s string) (string, os.Error) {
120121 return "" , nil
121122}
122123
123- func (tmpl * Template ) parsePartial (name string ) (* Template , os. Error ) {
124+ func (tmpl * Template ) parsePartial (name string ) (* Template , error ) {
124125 filenames := []string {
125126 path .Join (tmpl .dir , name ),
126127 path .Join (tmpl .dir , name + ".mustache" ),
@@ -132,14 +133,14 @@ func (tmpl *Template) parsePartial(name string) (*Template, os.Error) {
132133 var filename string
133134 for _ , name := range filenames {
134135 f , err := os .Open (name )
135- f .Close ()
136136 if err == nil {
137137 filename = name
138+ f .Close ()
138139 break
139140 }
140141 }
141142 if filename == "" {
142- return nil , os . NewError (fmt .Sprintf ("Could not find partial %q" , name ))
143+ return nil , errors . New (fmt .Sprintf ("Could not find partial %q" , name ))
143144 }
144145
145146 partial , err := ParseFile (filename )
@@ -151,11 +152,11 @@ func (tmpl *Template) parsePartial(name string) (*Template, os.Error) {
151152 return partial , nil
152153}
153154
154- func (tmpl * Template ) parseSection (section * sectionElement ) os. Error {
155+ func (tmpl * Template ) parseSection (section * sectionElement ) error {
155156 for {
156157 text , err := tmpl .readString (tmpl .otag )
157158
158- if err == os .EOF {
159+ if err == io .EOF {
159160 return parseError {section .startline , "Section " + section .name + " has no closing tag" }
160161 }
161162
@@ -168,7 +169,7 @@ func (tmpl *Template) parseSection(section *sectionElement) os.Error {
168169 text , err = tmpl .readString (tmpl .ctag )
169170 }
170171
171- if err == os .EOF {
172+ if err == io .EOF {
172173 //put the remaining text in a block
173174 return parseError {tmpl .curline , "unmatched open tag" }
174175 }
@@ -236,10 +237,10 @@ func (tmpl *Template) parseSection(section *sectionElement) os.Error {
236237 return nil
237238}
238239
239- func (tmpl * Template ) parse () os. Error {
240+ func (tmpl * Template ) parse () error {
240241 for {
241242 text , err := tmpl .readString (tmpl .otag )
242- if err == os .EOF {
243+ if err == io .EOF {
243244 //put the remaining text in a block
244245 tmpl .elems = append (tmpl .elems , & textElement {[]byte (text )})
245246 return nil
@@ -255,7 +256,7 @@ func (tmpl *Template) parse() os.Error {
255256 text , err = tmpl .readString (tmpl .ctag )
256257 }
257258
258- if err == os .EOF {
259+ if err == io .EOF {
259260 //put the remaining text in a block
260261 return parseError {tmpl .curline , "unmatched open tag" }
261262 }
@@ -536,7 +537,7 @@ func (tmpl *Template) RenderInLayout(layout *Template, context ...interface{}) s
536537 return layout .Render (allContext ... )
537538}
538539
539- func ParseString (data string ) (* Template , os. Error ) {
540+ func ParseString (data string ) (* Template , error ) {
540541 cwd := os .Getenv ("CWD" )
541542 tmpl := Template {data , "{{" , "}}" , 0 , 1 , cwd , []interface {}{}}
542543 err := tmpl .parse ()
@@ -548,7 +549,7 @@ func ParseString(data string) (*Template, os.Error) {
548549 return & tmpl , err
549550}
550551
551- func ParseFile (filename string ) (* Template , os. Error ) {
552+ func ParseFile (filename string ) (* Template , error ) {
552553 data , err := ioutil .ReadFile (filename )
553554 if err != nil {
554555 return nil , err
@@ -569,40 +570,40 @@ func ParseFile(filename string) (*Template, os.Error) {
569570func Render (data string , context ... interface {}) string {
570571 tmpl , err := ParseString (data )
571572 if err != nil {
572- return err .String ()
573+ return err .Error ()
573574 }
574575 return tmpl .Render (context ... )
575576}
576577
577578func RenderInLayout (data string , layoutData string , context ... interface {}) string {
578579 layoutTmpl , err := ParseString (layoutData )
579580 if err != nil {
580- return err .String ()
581+ return err .Error ()
581582 }
582583 tmpl , err := ParseString (data )
583584 if err != nil {
584- return err .String ()
585+ return err .Error ()
585586 }
586587 return tmpl .RenderInLayout (layoutTmpl , context ... )
587588}
588589
589590func RenderFile (filename string , context ... interface {}) string {
590591 tmpl , err := ParseFile (filename )
591592 if err != nil {
592- return err .String ()
593+ return err .Error ()
593594 }
594595 return tmpl .Render (context ... )
595596}
596597
597598func RenderFileInLayout (filename string , layoutFile string , context ... interface {}) string {
598599 layoutTmpl , err := ParseFile (layoutFile )
599600 if err != nil {
600- return err .String ()
601+ return err .Error ()
601602 }
602603
603604 tmpl , err := ParseFile (filename )
604605 if err != nil {
605- return err .String ()
606+ return err .Error ()
606607 }
607608 return tmpl .RenderInLayout (layoutTmpl , context ... )
608609}
0 commit comments