Search code examples
gogoogle-cloud-firestoreslicego-templatesgo-html-template

Why aren't the passed variables rendering in html/template?


I can't figure out why the passed variables aren't rendered with html/template

This is what is rendered in the Browser:

This is all the passed variables: [0xc0000a8ec0 0xc0000a8f80 0xc0000a9040 0xc0000a9100]

City Population State Country Capital

This is the log:
$ go run main.go

2019/11/27 11:00:39 **** => &city has &main.City{Name:"Washington D.C.", State:"", Country:"USA", Capital:false, Population:680000} before appending to cities ****
2019/11/27 11:00:39 **** => &city has &main.City{Name:"Los Angeles", State:"CA", Country:"USA", Capital:false, Population:3900000} before appending to cities ****
2019/11/27 11:00:39 **** => &city has &main.City{Name:"San Francisco", State:"CA", Country:"USA", Capital:false, Population:860000} before appending to cities ****
2019/11/27 11:00:39 **** => &city has &main.City{Name:"Tokyo", State:"", Country:"Japan", Capital:true, Population:9000000} before appending to cities ****
**** => cities outside {} has 4

Here is the handler function:

func indexHandler(w http.ResponseWriter, r *http.Request) {
    projectID := "XXXXXXXXXXXXXX"
    ctx := context.Background()

    client, _ := firestore.NewClient(ctx, projectID)

    query := client.Collection("cities").Documents(ctx)
    defer query.Stop()
    cities := make([]*City, 0)
    for {
        doc, err := query.Next()
        if err == iterator.Done {
            break
        }
        c := doc.Data()

        // is there an easier way to populate the city struct
        city := City{
            Name:       c["name"].(string),
            Country:    c["country"].(string),
            Population: c["population"].(int64),
        }
        // This is my ugly solution to dealing with nil value from Firestore
        _, ok := c["capital"]
        if ok {
            city.Capital = c["capital"].(bool)
        }
        state, ok := c["state"].(string)
        if ok {
            city.State = state
        }
        log.Printf("**** => &city has %#v before appending to cities ****", &city)
        cities = append(cities, &city)

    }

    fmt.Printf("**** => cities outside {} has %d", len(cities))
    indexTemplate.Execute(w, cities)    
}

This is the index.html template:

{% autoescape true %}

<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">

</head>
<body>
        <P>This is all the passed variables: {{ . }}</P>
    <table>
        <tr>  <td>City</td> <td>Population</td> <td> State</td><td>Country</td> <td>Capital</td> </tr>

    {{ range .cities }}
    <tr>  <td>testing</td> <td>testing</td> <td>testing</td><td>testing</td> <td>testing</td> </tr>
    <tr>  <td>{{.Name}}</td> <td>{{ .Population}}</td> <td>{{ .State}}</td><td>{{.Country}}</td> <td>{{.Capital}}</td> </tr>
    {{ end }}


</table>
{{ $cities := . }}

</body>
</html>
{% endautoescape %}

Solution

  • Never omit errors, check what indexTemplate.Execute(w, cities) returns. It probably reveals the cause:

    You pass cities as the data, which is a slice. There is no cities field or method of it. The passed data becomes the dot, so you have to range over the dot.

    Instead of:

    {{ range .cities }}
    

    Use:

    {{ range . }}