DEV Community

Ega Prasetya
Ega Prasetya

Posted on

Echo Go - Template Rendering

Essentially parsing and rendering template proccess is not to handle with echo but handle with API from package html/template. So way render template in echo is same like a real applications used go.

PRACTICE

Ok so to make it easier to understand go to practice right now and create file main.go on your text editor.

First step, we need import package html/template, io, and echo.

Alt Text

Create struct Renderer, this struct have a 3 property and 2 method.

Alt Text

Here`s the assignment and explanation of the third property above.

  • Property .template accountable for parsing and rendering template.
  • Property .location direct to path folder when file template stand.
  • Property .debug relocate type a bool.

Next, create function NewRenderer() for easier inisialitation objek renderer.

Alt Text

Create 2 method for struct Renderer is .ReloadTemplates() and .Render()

Alt Text

Next, create echo router, override property and create a route.

Alt Text

When we call newRenderer() used ./*.html to lead for all html file in current folder. Create index.html

Alt Text

Testing

Alt Text

Finally! Try at home.

Top comments (0)