Skip to content

Commit

Permalink
Allow nested structs
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Hoisie committed May 16, 2010
1 parent 4c02441 commit 12a5838
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
26 changes: 13 additions & 13 deletions mustache.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ func callMethod(data reflect.Value, name string) (result reflect.Value, found bo
for i := 0; i < nMethod; i++ {
method := typ.Method(i)
if method.Name == name {

found = true // we found the name regardless
// does receiver type match? (pointerness might be off)
if typ == method.Type.In(0) {
Expand Down Expand Up @@ -332,7 +333,6 @@ func renderSection(section *sectionElement, context reflect.Value, buf io.Writer

valueInd := reflect.Indirect(value)
//if the section is nil, we shouldn't do anything

var contexts = new(vector.Vector)

switch val := valueInd.(type) {
Expand All @@ -350,7 +350,7 @@ func renderSection(section *sectionElement, context reflect.Value, buf io.Writer
for i := 0; i < val.Len(); i++ {
contexts.Push(val.Elem(i))
}
case *reflect.MapValue:
case *reflect.MapValue, *reflect.StructValue:
contexts.Push(val)
default:
contexts.Push(context)
Expand Down Expand Up @@ -388,9 +388,16 @@ func (tmpl *Template) renderTemplate(context reflect.Value, buf io.Writer) {
}
}

func (tmpl *Template) Render(context interface{}, buf io.Writer) {
func (tmpl *Template) Render(context interface{}) string {
val := reflect.NewValue(context)
var buf bytes.Buffer
tmpl.renderTemplate(val, &buf)
return buf.String()
}

func (tmpl *Template) RenderWriter(context interface{}, writer io.Writer) {
val := reflect.NewValue(context)
tmpl.renderTemplate(val, buf)
tmpl.renderTemplate(val, writer)
}

func ParseString(data string) (*Template, os.Error) {
Expand All @@ -407,7 +414,6 @@ func ParseString(data string) (*Template, os.Error) {

func ParseFile(filename string) (*Template, os.Error) {
data, err := ioutil.ReadFile(filename)

if err != nil {
return nil, err
}
Expand All @@ -431,10 +437,7 @@ func Render(data string, context interface{}) (string, os.Error) {
return "", err
}

var buf bytes.Buffer
tmpl.Render(context, &buf)

return buf.String(), nil
return tmpl.Render(context), nil
}

func RenderFile(filename string, context interface{}) (string, os.Error) {
Expand All @@ -444,8 +447,5 @@ func RenderFile(filename string, context interface{}) (string, os.Error) {
return "", err
}

var buf bytes.Buffer
tmpl.Render(context, &buf)

return buf.String(), nil
return tmpl.Render(context), nil
}
12 changes: 12 additions & 0 deletions mustache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ type User struct {
Id int64
}

type settings struct {
Allow bool
}

func (u User) func1() string {
return u.Name
}
Expand All @@ -40,6 +44,11 @@ func (u *User) func4() (map[string]string, os.Error) {
return nil, nil
}

func (u *User) func5() (*settings, os.Error) {
return &settings{true}, nil
}


func (u User) truefunc1() bool {
return true
}
Expand All @@ -57,6 +66,7 @@ func makeVector(n int) *vector.Vector {
}

var tests = []Test{

Test{`hello {{name}}`, map[string]string{"name": "world"}, "hello world"},
Test{`{{a}}{{b}}{{c}}{{d}}`, map[string]string{"a": "a", "b": "b", "c": "c", "d": "d"}, "abcd"},
Test{`0{{a}}1{{b}}23{{c}}456{{d}}89`, map[string]string{"a": "a", "b": "b", "c": "c", "d": "d"}, "0a1b23c456d89"},
Expand Down Expand Up @@ -104,6 +114,8 @@ var tests = []Test{
Test{`{{#truefunc1}}abcd{{/truefunc1}}`, User{"Mike", 1}, "abcd"},
Test{`{{#truefunc1}}abcd{{/truefunc1}}`, &User{"Mike", 1}, "abcd"},
Test{`{{#truefunc2}}abcd{{/truefunc2}}`, &User{"Mike", 1}, "abcd"},

Test{`{{#func5}}{{#Allow}}abcd{{/Allow}}{{/func5}}`, &User{"Mike", 1}, "abcd"},
}

func TestBasic(t *testing.T) {
Expand Down

0 comments on commit 12a5838

Please sign in to comment.