title: Map Json Data To Html Template In GoLang
body: <p>I am looping through my API response and adding it to the html template like this,</p>
  
  <pre><code> // Following sends same information as above to the browser as html
       t, err := template.New("TopMovies").Parse(`
        {{define "TopMovies"}}
          &lt;html&gt;
          &lt;ul&gt;
          {{$ImgUrl := "http://image.tmdb.org/t/p/w185" }}
          {{range $movies := .Results}}
          &lt;li&gt;{{$ImgUrl}}{{$movies.PosterPath}}&lt;/li&gt;
          &lt;li&gt;{{$movies.Adult}}&lt;/li&gt;
          &lt;li&gt;{{$movies.Overview}}&lt;/li&gt;
          &lt;li&gt;{{$movies.ReleaseDate}}&lt;/li&gt;
          &lt;li&gt;{{$movies.GenreIds}}&lt;/li&gt;
          &lt;li&gt;{{$movies.Id}}&lt;/li&gt;
          &lt;li&gt;{{$movies.OriginalTitle}}&lt;/li&gt;
          &lt;li&gt;{{$movies.OriginalLanguage}}&lt;/li&gt;
          &lt;li&gt;{{$movies.Title}}&lt;/li&gt;
          &lt;li&gt;{{$ImgUrl}}{{$movies.BackdropPath}}&lt;/li&gt;
          &lt;li&gt;{{$movies.Popularity}}&lt;/li&gt;
          &lt;li&gt;{{$movies.VoteCount}}&lt;/li&gt;
          &lt;li&gt;{{$movies.Video}}&lt;/li&gt;
          &lt;li&gt;{{$movies.VoteAverage}}&lt;/li&gt;
          {{end}}
          &lt;/ul&gt;
          &lt;/html&gt;
        {{end}}
        `)
      err = t.ExecuteTemplate(w, "T", p) // This writes the client response
  }
  </code></pre>
  
  <p>I am under the impression I should be able to call this in my html templates like this,</p>
  
  <pre><code>{{.TopMovies}}
  </code></pre>
  
  <p>But when I run the app the data does not appear in the html page I am calling it in. What am I missing here?</p>
  
  <p>I create a struct like this,</p>
  
  <pre><code>//A Page structure
  type Page struct {
    Title string
    TopMovies string
  }
  </code></pre>
  
  <p>Then I create my handle like this,</p>
  
  <pre><code>func TopMoviesHandler(w http.ResponseWriter, r *http.Request) {
     res, err := http.Get(url)
        if err != nil {
          panic(err)
        }
        defer res.Body.Close()
  
        body, err := ioutil.ReadAll(res.Body)
        if err != nil {
          panic(err)
        }
        var p Payload
  
        err = json.Unmarshal(body, &amp;p)
        if err != nil {
          panic(err)
        }
  
    // Following sends same information as above to the browser as html
       t, err := template.New("TopMovies").Parse(`
        {{define "TopMovies"}}
          &lt;html&gt;
          &lt;ul&gt;
          {{$ImgUrl := "http://image.tmdb.org/t/p/w185" }}
          {{range $movies := .Results}}
          &lt;li&gt;{{$ImgUrl}}{{$movies.PosterPath}}&lt;/li&gt;
          &lt;li&gt;{{$movies.Adult}}&lt;/li&gt;
          &lt;li&gt;{{$movies.Overview}}&lt;/li&gt;
          &lt;li&gt;{{$movies.ReleaseDate}}&lt;/li&gt;
          &lt;li&gt;{{$movies.GenreIds}}&lt;/li&gt;
          &lt;li&gt;{{$movies.Id}}&lt;/li&gt;
          &lt;li&gt;{{$movies.OriginalTitle}}&lt;/li&gt;
          &lt;li&gt;{{$movies.OriginalLanguage}}&lt;/li&gt;
          &lt;li&gt;{{$movies.Title}}&lt;/li&gt;
          &lt;li&gt;{{$ImgUrl}}{{$movies.BackdropPath}}&lt;/li&gt;
          &lt;li&gt;{{$movies.Popularity}}&lt;/li&gt;
          &lt;li&gt;{{$movies.VoteCount}}&lt;/li&gt;
          &lt;li&gt;{{$movies.Video}}&lt;/li&gt;
          &lt;li&gt;{{$movies.VoteAverage}}&lt;/li&gt;
          {{end}}
          &lt;/ul&gt;
          &lt;/html&gt;
        {{end}}
        `)
      err = t.ExecuteTemplate(w, "T", p) // This writes the client response
  }
  </code></pre>
  
  <p>Then in <code>main.go</code></p>
  
  <pre><code>   http.HandleFunc("/TopPicks", TopMoviesHandler)
  
  TopPicks.html
  
  {{define "TopPicks"}}
      {{template "header" .}}
      &lt;div class="content"&gt;
      {{.TopMovies}}
      &lt;/div&gt;
       {{template "footer" .}}
      {{end}}
  </code></pre>
  
  <p>What does work is this,</p>
  
  <pre><code>func aboutHandler(w http.ResponseWriter, r *http.Request) {
    display(w, "about", &amp;Page{Title: "About"})
  }
  </code></pre>
  
  <p>I can add a title to the page in the same way as I previously mentioned but using <code>display()</code></p>
  
  <p>And in the html template</p>
  
  <pre><code>&lt;title&gt;{{.Title}}&lt;/title&gt;
  </code></pre>
  
  <p>How can I make this work for my json response?</p>