Reader small image

You're reading from  Echo Quick Start Guide

Product typeBook
Published inMay 2018
PublisherPackt
ISBN-139781789139433
Edition1st Edition
Tools
Right arrow
Author (1)
Ben Huson
Ben Huson
author image
Ben Huson

John Huson is an Echo developer
Read more about Ben Huson

Right arrow

Providing Templates and Static Content

A large portion of web application development involves the rendering of Hypertext Markup Language (HTML), as well as other dynamic and static content in order to provide an interface for which the users can interact with an application. Within this chapter, we will explore Echo's capabilities regarding static content delivery and create dynamic content that will be rendered on the server side using templates. We will take an inventory of how Echo can help us with the following tasks:

  • Serve static content and files from a particular directory on the server
  • Create dynamic templates from any template library
  • Interact with the Echo web application from within the templates

With these features at your disposal, you will be able to pull together a functional user experience quickly using server-side rendering of dynamic content. You will...

Technical requirements

Serving static files

When we talk about static files, we are talking about web application assets that are needed in order to render a working user experience. Static file assets include, but are not limited to, Cascading Style Sheet (CSS) files, JavaScript (JS) files, and any other file that a user would need to download in order to render the web application within the browser. Within Echo, we are able to present an interface for the browser to download these assets much in the same way we present new routes within Echo. The following is an example from our project where we are going to present a route called /static/ wherein every request that is made to any resource prefixed with /static/ will be served from a particular directory which is based on the location of the binary executable, in our example the ./static/ directory:

e.Static("/static", "static"...

Template basics

Many times in web applications, there is a need to have dynamic content rendered as well as static content. Typically, the dynamic content supplied to the caller is structured and includes dynamic data. Templates provide the developer with the option to lay out the structure of the response, as well as include syntax to allow for dynamic data. A template in our context is a file that has a preset format which is used as a starting point for a response to a user. Within this section, we will mainly focus on the basics of template syntax using the html/template package. Go has a text/template package as well as the html/template package. The former and later use the same template syntax, but the later provides safety features such as production of HTML output that is safe from code injection. It is important to make sure you use the html/template package when rendering...

Templates within Echo

The Echo framework does provide a facility for custom rendering which is based on the echo.Renderer interface. This interface stipulates that implementations have a method Render which will perform the rendering of data. With this convention, we are able to take what we learned about Go templates and apply those templates to implement a custom echo.Renderer that is capable of rendering HTML templates back to the caller. The following is a very minimal example of an echo.Renderer implementation that we have implemented in $GOPATH/src/github.com/PacktPublishing/Echo-Essentials/chapter8/handlers/reminder.go:

type CustomTemplate struct {
        *template.Template
}

func (ct *CustomTemplate) Render(w io.Writer, name string, data interface{},
        ctx echo.Context) error {
        return ct.ExecuteTemplate(w, name, data)
}

Within this code block, we are creating...

Calling Echo from templates

There are times when it is handy to access functionality and methods of the web framework from within a template being rendered. A primary use case of this is to figure out reverse URLs for rendering links within a page. Luckily, due to the fact that Go allows for functions as variables, we are able to do this quite easily. The first thing we have to do is formalize the tmplData anonymous struct to include a method that we would want to call from within the template. Go's template library supports execution of methods by name which allows us to call methods off of the data struct that is passed into the execute. With this in mind, we will expose a Reverse function in our template data struct in which we can pass in Echo’s Reverse function. The following code is located at $GOPATH/src/github.com/PacktPublishing/Echo-Essentials/chapter8/handlers...

Summary

As with any fully rounded web application framework, the developer should have the ability to serve static and dynamic assets. With Echo, you have the ability to serve static content with the Static Echo instance method, as well as the ability to serve individual files with the File Echo instance method.

Often, it is not enough merely to serve static content. Echo also provides the ability to serve dynamic templates, which can be populated with dynamic data, as shown in this chapter. Within this chapter, we have shown not only how to render dynamic data from templates, but also how to call back into Echo from within the templates themselves.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Echo Quick Start Guide
Published in: May 2018Publisher: PacktISBN-13: 9781789139433
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Ben Huson

John Huson is an Echo developer
Read more about Ben Huson