Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ import (
"golang.org/x/crypto/acme/autocert"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"

"html/template"
)

// Echo is the top-level framework instance.
Expand Down Expand Up @@ -392,6 +394,20 @@ func New() (e *Echo) {
return
}

type TemplateRenderer struct {
templates *template.Template
renderer := &TemplateRenderer{templates: templates}
}

func (t *TemplateRenderer) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
return t.templates.ExecuteTemplate(w, name, data)
}

func BeginTemplates(e *Echo) {
templates := template.Must(template.ParseGlob("templates/*.html"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • instead of Must (which panics) use functions that return errors and that function should return an error
  • instead of hardcoded "templates/*.html" pass it as function parameter
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code can be shortened to

// import  //	htmlTmpl "html/template" //	textTmpl "text/template" type TextTemplateRenderer struct { Templates *textTmpl.Template } func (t *TextTemplateRenderer) Render(w io.Writer, name string, data interface{}, c Context) error { return t.Templates.ExecuteTemplate(w, name, data) } type HTMLTemplateRenderer struct { Templates *htmlTmpl.Template } func (t *HTMLTemplateRenderer) Render(w io.Writer, name string, data interface{}, c Context) error { return t.Templates.ExecuteTemplate(w, name, data) }

and use as

e.Renderer = &echo.HTMLTemplateRenderer{ Templates: template.Must(template.ParseGlob("templates/*.html")),	}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I'm new in both of Go, and Echo.)
When I tried it as a test, I used my own repository and imported it, not compiled.
So I don't know why it returns an error.
This is a recommendation, this is an idea that you could implement, an idea that will make it a little bit more beginner-friendly, as the current way of middleware is not a very beginner-friendly way to make a simple app.
Thanks for reading,
@IsusRamzy

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've allowed edits by maintainers, so you could implement it the right way.

e.Renderer = renderer
}

// NewContext returns a Context instance.
func (e *Echo) NewContext(r *http.Request, w http.ResponseWriter) Context {
return &context{
Expand Down Expand Up @@ -977,6 +993,8 @@ func handlerName(h HandlerFunc) string {
return t.String()
}



// // PathUnescape is wraps `url.PathUnescape`
// func PathUnescape(s string) (string, error) {
// return url.PathUnescape(s)
Expand Down