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/reminder.go:

type TmplData struct {
        Reminders []Reminder
        Title     string
        rev       func(name string, params ...interface{}) string
}

func (td TmplData) Reverse(name string, params ...interface{}) string {
        return td.rev(name, params...)
}

We are able to take this type now and populate it with any reverse function we want as seen here:

   data := TmplData{reminders, "Reminders Page", c.Echo().Reverse}
        return c.Render(http.StatusOK, "reminders", data)

Now that we have our template being rendered with a Reverse method built into the template data structure we can use this new functionality to have our templates figure out URL link reversals. This is extremely beneficial when you need to have links generated from within a template. In order to perform this we need to name the routes we wish to have URL reversals for explicitly as shown below in cmd/service/main.go

e.POST("/login", handlers.Login).Name = "login"

After we have this route name in place, we need to update our template to add a link to the login named route. Below is the modified rendered template, which includes a line specifying that we would like to have a login link, and use the Reverse template data method we have created previously to render this link to the /login route:

<html>
<head>
<title>{{.Title}}</title>
</head>
<body>
<a href={{ .Reverse "login" }}>Login</a>
<table>
<th>
<td>Reminder Name</td>
<td>Reminder ID</td>
<td>Reminder Due</td>
</th>
{{ range .Reminders }}
<tr>
<td>{{ .Name }}</td>
<td>{{ .ID }}</td>
<td>{{ .Due }}</td>
</tr>
{{ else }}
<tr>
<td colspan=3>No rows!</td>
</tr>
{{ end }}
</body>
</html>

When this template is rendered the TmplData.Reverse method is called with the parameter "login" which in turn calls our Echo instance's Reverse function. This will look up a route with a name login and return the full route associated. Following is the rendered content of the template:

<html>
<head>
<title>Reminders Page</title>
</head>
<body>
<a href=/login>Login</a>
<table>
<th>
<td>Reminder Name</td>
<td>Reminder ID</td>
<td>Reminder Due</td>
</th>

<tr>
<td>Oil Change</td>
<td>7edc58cf-bd3f-48cb-b741-b2cafb106943</td>
<td>2018-08-12 22:49:02.562939274 -0400 EDT m=+7776002.527394939</td>
</tr>

<tr>
<td>Birthday Party</td>
<td>b6b0da75-bbd5-4d61-9090-c88b9ca0dcec</td>
<td>2020-01-01 00:00:00 +0000 UTC</td>
</tr>

</body>
</html>

As you can see, we have a new line in our template rendered output that shows <a href=/login>Login</a> which is a link to the login route. This very simple change has allowed us to inject the Echo instance's Reverse method into our template effectively. It is fairly trivial to add more functionality to this TmplData structure to allow for your templates to have access to any aspect of your application. This sort of flexibility will allow you to quickly create server-side rendered templates that make your application more dynamic and flexible.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.136.18.218