@@ -375,8 +375,8 @@ func lookup(contextChain []interface{}, name string) reflect.Value {
375375 }()
376376
377377Outer:
378- for i := len (contextChain ) - 1 ; i >= 0 ; i -- {
379- v := contextChain [ i ] .(reflect.Value )
378+ for _ , ctx := range contextChain { // i := len(contextChain) - 1; i >= 0; i-- {
379+ v := ctx .(reflect.Value )
380380 for v .IsValid () {
381381 typ := v .Type ()
382382 if n := v .Type ().NumMethod (); n > 0 {
@@ -473,13 +473,14 @@ func renderSection(section *sectionElement, contextChain []interface{}, buf io.W
473473 }
474474 }
475475
476+ chain2 := make ([]interface {}, len (contextChain )+ 1 )
477+ copy (chain2 [1 :], contextChain )
476478 //by default we execute the section
477479 for _ , ctx := range contexts {
478- contextChain = append ( contextChain , ctx )
480+ chain2 [ 0 ] = ctx
479481 for _ , elem := range section .elems {
480- renderElement (elem , contextChain , buf )
482+ renderElement (elem , chain2 , buf )
481483 }
482- contextChain = contextChain [0 : len (contextChain )- 1 ]
483484 }
484485}
485486
@@ -493,8 +494,8 @@ func renderElement(element interface{}, contextChain []interface{}, buf io.Write
493494 fmt .Printf ("Panic while looking up %q: %s\n " , elem .name , r )
494495 }
495496 }()
496-
497497 val := lookup (contextChain , elem .name )
498+
498499 if val .IsValid () {
499500 if elem .raw {
500501 fmt .Fprint (buf , val .Interface ())
@@ -529,7 +530,10 @@ func (tmpl *Template) Render(context ...interface{}) string {
529530
530531func (tmpl * Template ) RenderInLayout (layout * Template , context ... interface {}) string {
531532 content := tmpl .Render (context ... )
532- return layout .Render (map [string ]string {"content" :content })
533+ allContext := make ([]interface {}, len (context )+ 1 )
534+ copy (allContext [1 :], context )
535+ allContext [0 ] = map [string ]string {"content" : content }
536+ return layout .Render (allContext ... )
533537}
534538
535539func ParseString (data string ) (* Template , os.Error ) {
@@ -571,7 +575,7 @@ func Render(data string, context ...interface{}) string {
571575}
572576
573577func RenderInLayout (data string , layoutData string , context ... interface {}) string {
574- layoutTmpl ,err := ParseString (layoutData )
578+ layoutTmpl , err := ParseString (layoutData )
575579 if err != nil {
576580 return err .String ()
577581 }
@@ -591,7 +595,7 @@ func RenderFile(filename string, context ...interface{}) string {
591595}
592596
593597func RenderFileInLayout (filename string , layoutFile string , context ... interface {}) string {
594- layoutTmpl ,err := ParseFile (layoutFile )
598+ layoutTmpl , err := ParseFile (layoutFile )
595599 if err != nil {
596600 return err .String ()
597601 }
0 commit comments