Skip to content

Commit b2e21cd

Browse files
committed
Fixed bug in layout system, add more tests
1 parent 1bda2d4 commit b2e21cd

File tree

3 files changed

+42
-14
lines changed

3 files changed

+42
-14
lines changed

.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
*.6
2+
*.8
3+
*.o
4+
*.so
5+
*.out
6+
*.go~
7+
*.cgo?.*
8+
_cgo_*
9+
_obj
10+
_test
11+
_testmain.go
12+
*.swp

mustache.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,8 @@ func lookup(contextChain []interface{}, name string) reflect.Value {
375375
}()
376376

377377
Outer:
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

530531
func (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

535539
func ParseString(data string) (*Template, os.Error) {
@@ -571,7 +575,7 @@ func Render(data string, context ...interface{}) string {
571575
}
572576

573577
func 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

593597
func 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
}

mustache_test.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,16 @@ func makeVector(n int) *vector.Vector {
7070
return v
7171
}
7272

73-
var tests = []Test{
73+
type Category struct {
74+
Tag string
75+
Description string
76+
}
77+
78+
func (c Category) DisplayName() string {
79+
return c.Tag + " - " + c.Description
80+
}
7481

82+
var tests = []Test{
7583
{`hello world`, nil, "hello world"},
7684
{`hello {{name}}`, map[string]string{"name": "world"}, "hello world"},
7785
{`{{var}}`, map[string]string{"var": "5 > 2"}, "5 > 2"},
@@ -145,6 +153,9 @@ var tests = []Test{
145153
{`hello {{#section}}{{name}}{{/section}}`, map[string]interface{}{"name": "bob", "section": map[string]string{"name": "world"}}, "hello world"},
146154
{`hello {{#bool}}{{#section}}{{name}}{{/section}}{{/bool}}`, map[string]interface{}{"bool": true, "section": map[string]string{"name": "world"}}, "hello world"},
147155
{`{{#users}}{{canvas}}{{/users}}`, map[string]interface{}{"canvas": "hello", "users": []User{{"Mike", 1}}}, "hello"},
156+
{`{{#categories}}{{DisplayName}}{{/categories}}`, map[string][]*Category{
157+
"categories": {&Category{"a", "b"}},
158+
}, "a - b"},
148159
}
149160

150161
func TestBasic(t *testing.T) {
@@ -208,19 +219,20 @@ func TestMalformed(t *testing.T) {
208219
}
209220

210221
type LayoutTest struct {
211-
layout string
222+
layout string
212223
tmpl string
213224
context interface{}
214225
expected string
215226
}
216227

217228
var layoutTests = []LayoutTest{
218229
{`Header {{content}} Footer`, `Hello World`, nil, `Header Hello World Footer`},
219-
{`Header {{content}} Footer`, `Hello {{s}}`, map[string]string{"s":"World"}, `Header Hello World Footer`},
220-
{`Header {{content}} Footer`, `Hello {{content}}`, map[string]string{"content":"World"}, `Header Hello World Footer`},
230+
{`Header {{content}} Footer`, `Hello {{s}}`, map[string]string{"s": "World"}, `Header Hello World Footer`},
231+
{`Header {{content}} Footer`, `Hello {{content}}`, map[string]string{"content": "World"}, `Header Hello World Footer`},
232+
{`Header {{extra}} {{content}} Footer`, `Hello {{content}}`, map[string]string{"content": "World", "extra": "extra"}, `Header extra Hello World Footer`},
233+
{`Header {{content}} {{content}} Footer`, `Hello {{content}}`, map[string]string{"content": "World"}, `Header Hello World Hello World Footer`},
221234
}
222235

223-
224236
func TestLayout(t *testing.T) {
225237
for _, test := range layoutTests {
226238
output := RenderInLayout(test.tmpl, test.layout, test.context)

0 commit comments

Comments
 (0)