-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Pre-built Template Function #2673
base: master
Are you sure you want to change the base?
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")),
}
I have added a pre-built templates function to render HTML easily.