-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBuildResultsHTMLTemplate.go
More file actions
45 lines (40 loc) · 1.14 KB
/
BuildResultsHTMLTemplate.go
File metadata and controls
45 lines (40 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
import (
"bytes"
"html/template"
)
type TemplateData struct {
Results []FlightResult
SearchInput SearchInput
}
var layout = `<html>
<body>
Flights from {{.SearchInput.Departure}} to {{.SearchInput.Arrival}} on {{.SearchInput.Date}}
<table border='1' cellpadding='3px' style='border-collapse: collapse;'>
<tr>
<th>Route</th><th>Time</th><th>Airline</th><th>Price</th>
</tr>
{{range .Results}}
<tr>
<td>{{.Departure}} - {{.Arrival}}</td>
<td>{{.DepartureTime}} - {{.ArrivalTime}}</td>
<td>{{.Airline}}</td>
<td>{{.Price}}</td>
</tr>
{{end}}
</table>
</body>
</html>`
// BuildResultsHTMLTemplate build an HTML template for results and return it as string
func BuildResultsHTMLTemplate(results []FlightResult, searchInput SearchInput) []byte {
data := TemplateData{
Results: results,
SearchInput: searchInput,
}
var tmplBuffer bytes.Buffer
tmpl, err := template.New("results").Parse(layout)
LogFatalAndExitIfNotNull(err)
err = tmpl.Execute(&tmplBuffer, data)
LogFatalAndExitIfNotNull(err)
return tmplBuffer.Bytes()
}