@@ -2,7 +2,6 @@ package mustache
22
33import (
44 "bytes"
5- "container/vector"
65 "fmt"
76 "io"
87 "io/ioutil"
@@ -25,7 +24,7 @@ type sectionElement struct {
2524 name string
2625 inverted bool
2726 startline int
28- elems * vector. Vector
27+ elems [] interface {}
2928}
3029
3130type Template struct {
@@ -35,7 +34,7 @@ type Template struct {
3534 p int
3635 curline int
3736 dir string
38- elems * vector. Vector
37+ elems [] interface {}
3938}
4039
4140type parseError struct {
@@ -162,7 +161,7 @@ func (tmpl *Template) parseSection(section *sectionElement) os.Error {
162161
163162 // put text into an item
164163 text = text [0 : len (text )- len (tmpl .otag )]
165- section .elems . Push ( & textElement {[]byte (text )})
164+ section .elems = append ( section . elems , & textElement {[]byte (text )})
166165 if tmpl .p < len (tmpl .data ) && tmpl .data [tmpl .p ] == '{' {
167166 text , err = tmpl .readString ("}" + tmpl .ctag )
168167 } else {
@@ -194,12 +193,12 @@ func (tmpl *Template) parseSection(section *sectionElement) os.Error {
194193 tmpl .p += 2
195194 }
196195
197- se := sectionElement {name , tag [0 ] == '^' , tmpl .curline , new (vector. Vector ) }
196+ se := sectionElement {name , tag [0 ] == '^' , tmpl .curline , [] interface {}{} }
198197 err := tmpl .parseSection (& se )
199198 if err != nil {
200199 return err
201200 }
202- section .elems . Push ( & se )
201+ section .elems = append ( section . elems , & se )
203202 case '/' :
204203 name := strings .TrimSpace (tag [1 :])
205204 if name != section .name {
@@ -213,7 +212,7 @@ func (tmpl *Template) parseSection(section *sectionElement) os.Error {
213212 if err != nil {
214213 return err
215214 }
216- section .elems . Push ( partial )
215+ section .elems = append ( section . elems , partial )
217216 case '=' :
218217 if tag [len (tag )- 1 ] != '=' {
219218 return parseError {tmpl .curline , "Invalid meta tag" }
@@ -227,10 +226,10 @@ func (tmpl *Template) parseSection(section *sectionElement) os.Error {
227226 case '{' :
228227 if tag [len (tag )- 1 ] == '}' {
229228 //use a raw tag
230- section .elems . Push ( & varElement {tag [1 : len (tag )- 1 ], true })
229+ section .elems = append ( section . elems , & varElement {tag [1 : len (tag )- 1 ], true })
231230 }
232231 default :
233- section .elems . Push ( & varElement {tag , false })
232+ section .elems = append ( section . elems , & varElement {tag , false })
234233 }
235234 }
236235
@@ -240,16 +239,15 @@ func (tmpl *Template) parseSection(section *sectionElement) os.Error {
240239func (tmpl * Template ) parse () os.Error {
241240 for {
242241 text , err := tmpl .readString (tmpl .otag )
243-
244242 if err == os .EOF {
245243 //put the remaining text in a block
246- tmpl .elems . Push ( & textElement {[]byte (text )})
244+ tmpl .elems = append ( tmpl . elems , & textElement {[]byte (text )})
247245 return nil
248246 }
249247
250248 // put text into an item
251249 text = text [0 : len (text )- len (tmpl .otag )]
252- tmpl .elems . Push ( & textElement {[]byte (text )})
250+ tmpl .elems = append ( tmpl . elems , & textElement {[]byte (text )})
253251
254252 if tmpl .p < len (tmpl .data ) && tmpl .data [tmpl .p ] == '{' {
255253 text , err = tmpl .readString ("}" + tmpl .ctag )
@@ -280,12 +278,12 @@ func (tmpl *Template) parse() os.Error {
280278 tmpl .p += 2
281279 }
282280
283- se := sectionElement {name , tag [0 ] == '^' , tmpl .curline , new (vector. Vector ) }
281+ se := sectionElement {name , tag [0 ] == '^' , tmpl .curline , [] interface {}{} }
284282 err := tmpl .parseSection (& se )
285283 if err != nil {
286284 return err
287285 }
288- tmpl .elems . Push ( & se )
286+ tmpl .elems = append ( tmpl . elems , & se )
289287 case '/' :
290288 return parseError {tmpl .curline , "unmatched close tag" }
291289 case '>' :
@@ -294,7 +292,7 @@ func (tmpl *Template) parse() os.Error {
294292 if err != nil {
295293 return err
296294 }
297- tmpl .elems . Push ( partial )
295+ tmpl .elems = append ( tmpl . elems , partial )
298296 case '=' :
299297 if tag [len (tag )- 1 ] != '=' {
300298 return parseError {tmpl .curline , "Invalid meta tag" }
@@ -308,10 +306,10 @@ func (tmpl *Template) parse() os.Error {
308306 case '{' :
309307 //use a raw tag
310308 if tag [len (tag )- 1 ] == '}' {
311- tmpl .elems . Push ( & varElement {tag [1 : len (tag )- 1 ], true })
309+ tmpl .elems = append ( tmpl . elems , & varElement {tag [1 : len (tag )- 1 ], true })
312310 }
313311 default :
314- tmpl .elems . Push ( & varElement {tag , false })
312+ tmpl .elems = append ( tmpl . elems , & varElement {tag , false })
315313 }
316314 }
317315
@@ -369,16 +367,16 @@ func call(v reflect.Value, method reflect.Method) reflect.Value {
369367
370368// Evaluate interfaces and pointers looking for a value that can look up the name, via a
371369// struct field, method, or map key, and return the result of the lookup.
372- func lookup (contextChain * vector. Vector , name string ) reflect.Value {
370+ func lookup (contextChain [] interface {} , name string ) reflect.Value {
373371 defer func () {
374372 if r := recover (); r != nil {
375373 fmt .Printf ("Panic while looking up %q: %s\n " , name , r )
376374 }
377375 }()
378376
379377Outer:
380- for i := contextChain . Len ( ) - 1 ; i >= 0 ; i -- {
381- v := contextChain . At ( i ) .(reflect.Value )
378+ for i := len ( contextChain ) - 1 ; i >= 0 ; i -- {
379+ v := contextChain [ i ] .(reflect.Value )
382380 for v .IsValid () {
383381 typ := v .Type ()
384382 if n := v .Type ().NumMethod (); n > 0 {
@@ -449,10 +447,10 @@ loop:
449447 return v
450448}
451449
452- func renderSection (section * sectionElement , contextChain * vector. Vector , buf io.Writer ) {
450+ func renderSection (section * sectionElement , contextChain [] interface {} , buf io.Writer ) {
453451 value := lookup (contextChain , section .name )
454- var context = contextChain . At (contextChain . Len () - 1 ) .(reflect.Value )
455- var contexts = new (vector. Vector )
452+ var context = contextChain [ len (contextChain ) - 1 ] .(reflect.Value )
453+ var contexts = [] interface {}{}
456454 // if the value is nil, check if it's an inverted section
457455 isNil := isNil (value )
458456 if isNil && ! section .inverted || ! isNil && section .inverted {
@@ -462,31 +460,31 @@ func renderSection(section *sectionElement, contextChain *vector.Vector, buf io.
462460 switch val := valueInd ; val .Kind () {
463461 case reflect .Slice :
464462 for i := 0 ; i < val .Len (); i ++ {
465- contexts . Push ( val .Index (i ))
463+ contexts = append ( contexts , val .Index (i ))
466464 }
467465 case reflect .Array :
468466 for i := 0 ; i < val .Len (); i ++ {
469- contexts . Push ( val .Index (i ))
467+ contexts = append ( contexts , val .Index (i ))
470468 }
471469 case reflect .Map , reflect .Struct :
472- contexts . Push ( value )
470+ contexts = append ( contexts , value )
473471 default :
474- contexts . Push ( context )
472+ contexts = append ( contexts , context )
475473 }
476474 }
477475
478476 //by default we execute the section
479- for j := 0 ; j < contexts .Len (); j ++ {
480- ctx := contexts .At (j ).(reflect.Value )
481- contextChain .Push (ctx )
482- for i := 0 ; i < section .elems .Len (); i ++ {
483- renderElement (section .elems .At (i ), contextChain , buf )
477+ for _ , ctx := range contexts {
478+ contextChain = append (contextChain , ctx )
479+ for _ , elem := range section .elems {
480+ renderElement (elem , contextChain , buf )
484481 }
485- contextChain .Pop ()
482+ //either 1 or 2
483+ contextChain = contextChain [0 : len (contextChain )- 1 ]
486484 }
487485}
488486
489- func renderElement (element interface {}, contextChain * vector. Vector , buf io.Writer ) {
487+ func renderElement (element interface {}, contextChain [] interface {} , buf io.Writer ) {
490488 switch elem := element .(type ) {
491489 case * textElement :
492490 buf .Write (elem .text )
@@ -513,26 +511,26 @@ func renderElement(element interface{}, contextChain *vector.Vector, buf io.Writ
513511 }
514512}
515513
516- func (tmpl * Template ) renderTemplate (contextChain * vector. Vector , buf io.Writer ) {
517- for i := 0 ; i < tmpl .elems . Len (); i ++ {
518- renderElement (tmpl . elems . At ( i ) , contextChain , buf )
514+ func (tmpl * Template ) renderTemplate (contextChain [] interface {} , buf io.Writer ) {
515+ for _ , elem := range tmpl .elems {
516+ renderElement (elem , contextChain , buf )
519517 }
520518}
521519
522520func (tmpl * Template ) Render (context ... interface {}) string {
523521 var buf bytes.Buffer
524- var contextChain vector. Vector
522+ var contextChain [] interface {}
525523 for _ , c := range context {
526524 val := reflect .ValueOf (c )
527- contextChain . Push ( val )
525+ contextChain = append ( contextChain , val )
528526 }
529- tmpl .renderTemplate (& contextChain , & buf )
527+ tmpl .renderTemplate (contextChain , & buf )
530528 return buf .String ()
531529}
532530
533531func ParseString (data string ) (* Template , os.Error ) {
534532 cwd := os .Getenv ("CWD" )
535- tmpl := Template {data , "{{" , "}}" , 0 , 1 , cwd , new (vector. Vector ) }
533+ tmpl := Template {data , "{{" , "}}" , 0 , 1 , cwd , [] interface {}{} }
536534 err := tmpl .parse ()
537535
538536 if err != nil {
@@ -550,7 +548,7 @@ func ParseFile(filename string) (*Template, os.Error) {
550548
551549 dirname , _ := path .Split (filename )
552550
553- tmpl := Template {string (data ), "{{" , "}}" , 0 , 1 , dirname , new (vector. Vector ) }
551+ tmpl := Template {string (data ), "{{" , "}}" , 0 , 1 , dirname , [] interface {}{} }
554552 err = tmpl .parse ()
555553
556554 if err != nil {
@@ -562,11 +560,9 @@ func ParseFile(filename string) (*Template, os.Error) {
562560
563561func Render (data string , context ... interface {}) string {
564562 tmpl , err := ParseString (data )
565-
566563 if err != nil {
567564 return err .String ()
568565 }
569-
570566 return tmpl .Render (context ... )
571567}
572568
0 commit comments