Skip to content

Conversation

@IsusRamzy
Copy link

I have added a pre-built templates function to render HTML easily.

@IsusRamzy
Copy link
Author

Hello!

@IsusRamzy IsusRamzy changed the title Update echo.go Pre-built Template Function Aug 15, 2024
@IsusRamzy
Copy link
Author

Can you please merge?

@IsusRamzy
Copy link
Author

Hi!

Copy link
Contributor

@aldas aldas left a 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.

Copy link
Contributor

@aldas aldas left a 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"))
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.

@aldas
Copy link
Contributor

aldas commented Oct 20, 2024

I closed this PR and implemented struct to help creating renders in #2690

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants