-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Pre-built Template Function #2673
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
Conversation
|
Hello! |
|
Can you please merge? |
|
Hi! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code does not compile. Please fit it before I even review it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nb: fix compilation errors
| } | ||
|
|
||
| func BeginTemplates(e *Echo) { | ||
| templates := template.Must(template.ParseGlob("templates/*.html")) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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")),
}There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
|
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.