Pre-built Template Function#2673
Pre-built Template Function#2673IsusRamzy wants to merge 2 commits intolabstack:masterfrom IsusRamzy:patch-1
Conversation
|
Hello! |
|
Can you please merge? |
|
Hi! |
aldas
left a comment
There was a problem hiding this comment.
This code does not compile. Please fit it before I even review it.
aldas
left a comment
There was a problem hiding this comment.
nb: fix compilation errors
| } | ||
|
|
||
| func BeginTemplates(e *Echo) { | ||
| templates := template.Must(template.ParseGlob("templates/*.html")) |
There was a problem hiding this comment.
- 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
There was a problem hiding this comment.
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")),
}There was a problem hiding this comment.
(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
There was a problem hiding this comment.
I've allowed edits by maintainers, so you could implement it the right way.
|
I closed this PR and implemented struct to help creating renders in #2690 |
I have added a pre-built templates function to render HTML easily.