this might be a very basic question as I am not familiar with web programming at all. I am trying to create a table using html template which content is filled through a go script. My problem is that the tag does not fall where I intended it to. This is the html used for the html template
<h1>Exchange Rate for {{.Title}}</h1>
<form action="/dateShow/react" method="POST">
<table>
<tr>
<td>From</td>
<td>To</td>
<td>Rate</td>
<td>7-day avg</td>
</tr>{{.Data}}
</table>
<div><input type="submit" value="Save"></div>
</form>
I was expecting that the GO script would print the "data" after the first "tr"tag and inside the "table" tag. Instead, this is what I get
<html><head></head><body><h1>Exchange Rate for 2018-07-02</h1>
<form action="/dateShow/react" method="POST">
<tr><td>usd</td><td>idr</td><td>15.000000</td><td>14.238334</td></tr><tr><td>usd</td><td>sgd</td><td>1.320000</td><td>1.310000</td></tr>
<table>
<tbody><tr>
<td>From</td>
<td>To</td>
<td>Rate</td>
<td>7-day avg</td>
</tr></tbody></table>
<div><input type="submit" value="Save"></div>
</form></body></html>
This is so weird to be as the "title" part works fine and landed Where I want it to. But not the case for the body
Just pass the entire slice of exchange rates to the template, and use range
to iterate over it.
For example, if you have a struct like:
var ExchangeRates []ExchangeRate
type ExchangeRate struct {
From string
To string
Rate float64
SevenDayAverage float64
}
You will pass ExchangeRates
to the template, and the template might look like:
<table>
<tbody><tr>
<td>From</td>
<td>To</td>
<td>Rate</td>
<td>7-day avg</td>
</tr>
{{range .ExchangeRates}}
<tr>
<td>{{.From}}</td>
<td>{{.To}}</td>
<td>{{.Rate}}</td>
<td>{{.SevenDayAverage}}</td>
</tr>
{{end}}
</tbody>
</table>