Skip to content

Instantly share code, notes, and snippets.

@jfjensen
Last active February 23, 2024 10:27
Show Gist options
  • Save jfjensen/bdca6211734ce86a65324824ced9d498 to your computer and use it in GitHub Desktop.
Save jfjensen/bdca6211734ce86a65324824ced9d498 to your computer and use it in GitHub Desktop.
The main Go program for generating HTML code
package main
import (
"bufio"
"bytes"
"html/template"
"os"
)
type product struct {
Img string
Name string
Price string
Stars float64
Reviews int
Description string
}
func subtr(a, b float64) float64 {
return a - b
}
func list(e ...float64) []float64 {
return e
}
func main() {
data := []product{
{"images/1.png", "strawberries", "$2.00", 4.0, 251, "Lorem ipsum dolor sit amet, consectetur adipiscing elit."},
{"images/2.png", "onions", "$2.80", 5.0, 123, "Morbi sit amet erat vitae purus consequat vehicula nec sit amet purus."},
{"images/3.png", "tomatoes", "$3.10", 4.5, 235, "Curabitur tristique odio et nibh auctor, ut sollicitudin justo condimentum."},
{"images/4.png", "courgette", "$1.20", 4.0, 251, "Phasellus at leo a purus consequat ornare ac aliquam quam."},
{"images/5.png", "broccoli", "$3.80", 3.5, 123, "Maecenas sed ante sagittis, dapibus dui quis, hendrerit orci."},
{"images/6.png", "potatoes", "$3.00", 2.5, 235, "Vivamus malesuada est et tellus porta, vel consectetur orci dapibus."},
}
allFiles := []string{"content.tmpl", "footer.tmpl", "header.tmpl", "page.tmpl"}
var allPaths []string
for _, tmpl := range allFiles {
allPaths = append(allPaths, "./templates/"+tmpl)
}
templates := template.Must(template.New("").Funcs(template.FuncMap{"subtr": subtr, "list": list}).ParseFiles(allPaths...))
var processed bytes.Buffer
templates.ExecuteTemplate(&processed, "page", data)
outputPath := "./static/index.html"
f, _ := os.Create(outputPath)
w := bufio.NewWriter(f)
w.WriteString(string(processed.Bytes()))
w.Flush()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment