-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TemplateRenderer struct to ease creating renderers for `html/temp…
…late` and `text/template` packages.
- Loading branch information
Showing
3 changed files
with
57 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package echo | ||
|
||
import "io" | ||
|
||
// Renderer is the interface that wraps the Render function. | ||
type Renderer interface { | ||
Render(io.Writer, string, interface{}, Context) error | ||
} | ||
|
||
// TemplateRenderer is helper to ease creating renderers for `html/template` and `text/template` packages. | ||
// Example usage: | ||
// | ||
// e.Renderer = &echo.TemplateRenderer{ | ||
// Template: template.Must(template.ParseGlob("templates/*.html")), | ||
// } | ||
// | ||
// e.Renderer = &echo.TemplateRenderer{ | ||
// Template: template.Must(template.New("hello").Parse("Hello, {{.}}!")), | ||
// } | ||
type TemplateRenderer struct { | ||
Template interface { | ||
ExecuteTemplate(wr io.Writer, name string, data any) error | ||
} | ||
} | ||
|
||
// Render renders the template with given data. | ||
func (t *TemplateRenderer) Render(w io.Writer, name string, data interface{}, c Context) error { | ||
return t.Template.ExecuteTemplate(w, name, data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package echo | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"html/template" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestRenderWithTemplateRenderer(t *testing.T) { | ||
e := New() | ||
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(userJSON)) | ||
rec := httptest.NewRecorder() | ||
|
||
c := e.NewContext(req, rec) | ||
|
||
e.Renderer = &TemplateRenderer{ | ||
Template: template.Must(template.New("hello").Parse("Hello, {{.}}!")), | ||
} | ||
|
||
err := c.Render(http.StatusOK, "hello", "Jon Snow") | ||
if assert.NoError(t, err) { | ||
assert.Equal(t, http.StatusOK, rec.Code) | ||
assert.Equal(t, "Hello, Jon Snow!", rec.Body.String()) | ||
} | ||
} |