11 HTML templating with a gRPC client

 

This chapter covers

  • Creating a gRPC client
  • Using templates in Go to fill an HTML page
  • Calling an existing backend

When you have data that you want to show, there are two main questions to answer: What should you show, and how should you show it? While answering the first question is part of understanding the problem, knowing how to present the results, and making them clear, is often a requirement to make the solution meaningful. As computer scientists, we deal with huge amounts of data, most of which is mundane to the people who live next door, the other school parents, or the person in front of us at the grocery store. In chapter 5, we explained how using icons made the game Wordle (and our game Gordle) more entertaining than a display of mere numbers. Being able to clearly give a picture of the raw data is so important that the Golang developers introduced the text/template package to support this in the first version of the language. The goal of this package and of its sibling http/template is to generate enriched text output based on the contents of some data. Most of the time, the data needs to be nicely formatted—add a tab here, insert a new line there, or add this or that tag—and we’ll explain why other Golang tools aren’t better at this task than templates.

11.1 Your basic HTTP server

11.1.1 Hello plain text

11.1.2 Templates: Display a variable

11.1.3 Add a gRPC client

11.2 Basic template operations

11.2.1 Iterate through a slice

11.2.2 Accessing fields

11.2.3 Conditional formatting

11.3 Send a tick to the server

11.3.1 What page should we display?

11.3.2 Send tick to the backend

11.3.3 Adding colors

11.4 Use a form to create a habit

11.4.1 HTML form

11.4.2 Read a form’s values

11.5 More template niceties

11.5.1 Passing more than one object to the template

11.5.2 Calling functions