Follow

Follow

Rendering Responses in Echo.

Maheshwar Ligade's photo
Maheshwar Ligade
·Apr 22, 2023·

3 min read

Play this article

Table of contents

  • HTML Templates with Echo
  • JSON Responses with Echo
  • Serving Static Files with Echo
  • Conclusion

Echo is a popular Go web framework that provides a simple and efficient way to build web applications. One of the core functionalities of a web framework is to render responses to client requests. In this article, we will explore how to render responses in Echo. We will look at how to use HTML templates, return JSON responses, and serve static files.

HTML Templates with Echo

HTML templates are a convenient way to generate dynamic HTML pages. Echo supports HTML templates out of the box, and it provides a simple and efficient way to use them in your web application.

To use HTML templates with Echo, you need to create a template directory and put your HTML templates in it. You can then use the echo.Renderer middleware to render the templates.

Here's an example of how to set up the renderer middleware:

e := echo.New()

// Set the renderer middleware
e.Renderer = &Template{
    templates: template.Must(template.ParseGlob("templates/*.html")),
}

type Template struct {
    templates *template.Template
}

func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
    return t.templates.ExecuteTemplate(w, name, data)
}

In the above example, we create a new instance of echo.Echo and set the echo.Renderer middleware. We define a Template struct that holds our HTML templates. The Render function is called by Echo when it needs to render a template. In the Render function, we use the template.ExecuteTemplate function to render the requested template.

To use a template in a handler function, you simply call the c.Render function:

func hello(c echo.Context) error {
    data := map[string]interface{}{
        "name": "John",
    }
    return c.Render(http.StatusOK, "hello.html", data)
}

In the above example, we define a handler function that returns a rendered template. We pass the template name and data to the c.Render function. Echo will use the echo.Renderer middleware to render the template.

JSON Responses with Echo

JSON is a popular data format used for web APIs. Echo provides a simple way to return JSON responses to client requests.

To return a JSON response, you simply call the c.JSON function:

func hello(c echo.Context) error {
    data := map[string]interface{}{
        "message": "Hello, World!",
    }
    return c.JSON(http.StatusOK, data)
}

In the above example, we define a handler function that returns a JSON response. We pass the HTTP status code and the data to the c.JSON function. Echo will set the Content-Type header to application/json and serialize the data as JSON.

Serving Static Files with Echo

Static files, such as images and stylesheets, are an essential part of a web application. Echo provides a simple way to serve static files to client requests.

To serve static files, you use the echo.Static middleware:

e := echo.New()

// Serve static files
e.Static("/static", "static")

In the above example, we create a new instance of echo.Echo and use the echo.Static middleware to serve static files. We define a URL prefix (/static) and a directory path (static). Echo will serve any files in the static directory at the URL prefix.

Conclusion

In this article, we explored how to render responses in Echo. We looked at how to use HTML templates, return JSON responses, and serve static files. Echo provides a simple and efficient way to handle responses in your web application.

I hope this helps, you!!

More such articles:

https://medium.com/techwasti

https://www.youtube.com/channel/UCiTaHm1AYqMS4F4L9zyO7qA

https://www.techwasti.com/

\==========================**=========================

If this article adds any value to you then please clap and comment.

Let’s connect on Stackoverflow, LinkedIn, & Twitter.

Did you find this article valuable?

Support techwasti by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
 
Share this