Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return errors from Render, etc #58

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions mustache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package mustache

import (
"bytes"
"errors"
"fmt"
"html/template"
"io"
Expand Down Expand Up @@ -115,7 +114,7 @@ func (tmpl *Template) parsePartial(name string) (*Template, error) {
}
}
if filename == "" {
return nil, errors.New(fmt.Sprintf("Could not find partial %q", name))
return nil, fmt.Errorf("Could not find partial %q", name)
}

partial, err := ParseFile(filename)
Expand Down Expand Up @@ -557,43 +556,43 @@ func ParseFile(filename string) (*Template, error) {
return &tmpl, nil
}

func Render(data string, context ...interface{}) string {
func Render(data string, context ...interface{}) (string, error) {
tmpl, err := ParseString(data)
if err != nil {
return err.Error()
return "", err
}
return tmpl.Render(context...)
return tmpl.Render(context...), nil
}

func RenderInLayout(data string, layoutData string, context ...interface{}) string {
func RenderInLayout(data string, layoutData string, context ...interface{}) (string, error) {
layoutTmpl, err := ParseString(layoutData)
if err != nil {
return err.Error()
return "", err
}
tmpl, err := ParseString(data)
if err != nil {
return err.Error()
return "", err
}
return tmpl.RenderInLayout(layoutTmpl, context...)
return tmpl.RenderInLayout(layoutTmpl, context...), nil
}

func RenderFile(filename string, context ...interface{}) string {
func RenderFile(filename string, context ...interface{}) (string, error) {
tmpl, err := ParseFile(filename)
if err != nil {
return err.Error()
return "", err
}
return tmpl.Render(context...)
return tmpl.Render(context...), nil
}

func RenderFileInLayout(filename string, layoutFile string, context ...interface{}) string {
func RenderFileInLayout(filename string, layoutFile string, context ...interface{}) (string, error) {
layoutTmpl, err := ParseFile(layoutFile)
if err != nil {
return err.Error()
return "", err
}

tmpl, err := ParseFile(filename)
if err != nil {
return err.Error()
return "", err
}
return tmpl.RenderInLayout(layoutTmpl, context...)
return tmpl.RenderInLayout(layoutTmpl, context...), nil
}
36 changes: 24 additions & 12 deletions mustache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,6 @@ var tests = []Test{
"categories": {&Category{"a", "b"}},
}, "a - b"},

//invalid syntax - https://github.com/hoisie/mustache/issues/10
{`{{#a}}{{#b}}{{/a}}{{/b}}}`, map[string]interface{}{}, "line 1: interleaved closing tag: a"},

//dotted names(dot notation)
{`"{{person.name}}" == "{{#person}}{{name}}{{/person}}"`, map[string]interface{}{"person": map[string]string{"name": "Joe"}}, `"Joe" == "Joe"`},
{`"{{{person.name}}}" == "{{#person}}{{{name}}}{{/person}}"`, map[string]interface{}{"person": map[string]string{"name": "Joe"}}, `"Joe" == "Joe"`},
Expand All @@ -177,7 +174,10 @@ var tests = []Test{

func TestBasic(t *testing.T) {
for _, test := range tests {
output := Render(test.tmpl, test.context)
output, err := Render(test.tmpl, test.context)
if err != nil {
t.Fatalf("%q expected %q got error %q", test.tmpl, test.expected, err.Error())
}
if output != test.expected {
t.Fatalf("%q expected %q got %q", test.tmpl, test.expected, output)
}
Expand All @@ -187,7 +187,10 @@ func TestBasic(t *testing.T) {
func TestFile(t *testing.T) {
filename := path.Join(path.Join(os.Getenv("PWD"), "tests"), "test1.mustache")
expected := "hello world"
output := RenderFile(filename, map[string]string{"name": "world"})
output, err := RenderFile(filename, map[string]string{"name": "world"})
if err != nil {
t.Fatalf("testfile expected %q got error %q", expected, err.Error())
}
if output != expected {
t.Fatalf("testfile expected %q got %q", expected, output)
}
Expand All @@ -197,7 +200,10 @@ func TestPartial(t *testing.T) {
filename := path.Join(path.Join(os.Getenv("PWD"), "tests"), "test2.mustache")
println(filename)
expected := "hello world"
output := RenderFile(filename, map[string]string{"Name": "world"})
output, err := RenderFile(filename, map[string]string{"Name": "world"})
if err != nil {
t.Fatalf("testpartial expected %q got error %q", expected, err.Error())
}
if output != expected {
t.Fatalf("testpartial expected %q got %q", expected, output)
}
Expand All @@ -215,8 +221,8 @@ func TestSectionPartial(t *testing.T) {
}
*/
func TestMultiContext(t *testing.T) {
output := Render(`{{hello}} {{World}}`, map[string]string{"hello": "hello"}, struct{ World string }{"world"})
output2 := Render(`{{hello}} {{World}}`, struct{ World string }{"world"}, map[string]string{"hello": "hello"})
output, _ := Render(`{{hello}} {{World}}`, map[string]string{"hello": "hello"}, struct{ World string }{"world"})
output2, _ := Render(`{{hello}} {{World}}`, struct{ World string }{"world"}, map[string]string{"hello": "hello"})
if output != "hello world" || output2 != "hello world" {
t.Fatalf("TestMultiContext expected %q got %q", "hello world", output)
}
Expand All @@ -227,13 +233,16 @@ var malformed = []Test{
{`{{}}`, nil, "empty tag"},
{`{{}`, nil, "unmatched open tag"},
{`{{`, nil, "unmatched open tag"},

//invalid syntax - https://github.com/hoisie/mustache/issues/10
{`{{#a}}{{#b}}{{/a}}{{/b}}}`, map[string]interface{}{}, "line 1: interleaved closing tag: a"},
}

func TestMalformed(t *testing.T) {
for _, test := range malformed {
output := Render(test.tmpl, test.context)
if strings.Index(output, test.expected) == -1 {
t.Fatalf("%q expected %q in error %q", test.tmpl, test.expected, output)
_, err := Render(test.tmpl, test.context)
if strings.Index(err.Error(), test.expected) == -1 {
t.Fatalf("%q expected %q in error %q", test.tmpl, test.expected, err.Error())
}
}
}
Expand All @@ -255,7 +264,10 @@ var layoutTests = []LayoutTest{

func TestLayout(t *testing.T) {
for _, test := range layoutTests {
output := RenderInLayout(test.tmpl, test.layout, test.context)
output, err := RenderInLayout(test.tmpl, test.layout, test.context)
if err != nil {
t.Fatalf("%q expected %q got error %q", test.tmpl, test.expected, err.Error())
}
if output != test.expected {
t.Fatalf("%q expected %q got %q", test.tmpl, test.expected, output)
}
Expand Down