Skip to content

Instantly share code, notes, and snippets.

@shiyanhui
Created April 26, 2017 13:23
Show Gist options
  • Save shiyanhui/3c904a1a31ad1666b001de11300e0d18 to your computer and use it in GitHub Desktop.
Save shiyanhui/3c904a1a31ad1666b001de11300e0d18 to your computer and use it in GitHub Desktop.
// index.html
<html>
<head>
</head>
<body>
<%@ body { %>
<% } %>
</body>
</html>
// page.html
<%: func HandleMetrics(animals []Animal, w io.Writer) %>
<%~ "index.html" %>
<%@ body { %>
<% for _, animal := range animals { %>
<tr>
<td class="dinoname"><%=s animal.Nickname %></td>
<td><%=i animal.Zone %></td>
<td><%=i animal.Age %></td>
<td class="dinodata"></td>
</tr>
<% } %>
<% } %>
// page.html.go
package main
import (
"io"
"github.com/shiyanhui/hero"
)
func HandleMetrics(animals []Animal, w io.Writer) {
_buffer := hero.GetBuffer()
defer hero.PutBuffer(_buffer)
_buffer.WriteString(`<html>
<head>
</head>
<body>
`)
for _, animal := range animals {
_buffer.WriteString(`
<tr>
<td class="dinoname">`)
hero.EscapeHTML(animal.Nickname, _buffer)
_buffer.WriteString(`</td>
<td>`)
hero.FormatInt(int64(animal.Zone), _buffer)
_buffer.WriteString(`</td>
<td>`)
hero.FormatInt(int64(animal.Age), _buffer)
_buffer.WriteString(`</td>
<td class="dinodata"></td>
</tr>
`)
}
_buffer.WriteString(`
</body>
</html>
`)
w.Write(_buffer.Bytes())
}
// main.go
package main
type Animal struct {
ID int
AnimalType string
Nickname string
Zone int
Age int
}
func main() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment