Skip to content

Instantly share code, notes, and snippets.

@0x1F602
Created September 30, 2015 04:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0x1F602/c7f634a164e5a6180270 to your computer and use it in GitHub Desktop.
Save 0x1F602/c7f634a164e5a6180270 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"os"
"io/ioutil"
"text/template"
)
func readLines(path string) (string, error) {
content, err := ioutil.ReadFile(path)
if err != nil {
fmt.Printf("Error reading lines\n")
return "", err
}
return string(content[:]), nil
}
type HTMLTemplate struct {
Content string
}
func parseTemplate(template_filepath string, html_filepath string, passed_content string) (error) {
template_content, err := readLines(template_filepath)
if err != nil {
return err
}
tmpl, err := template.New("webpage").Parse(template_content)
if err != nil {
fmt.Printf("Error generating template\n")
return err
}
f, err := os.Create(html_filepath)
if err != nil {
fmt.Println(err)
return err
}
html_template_interface := HTMLTemplate{passed_content}
tmpl.Execute(f,html_template_interface)
return nil
}
func main() {
content, err := readLines("templates/index.tmpl")
if err != nil {
fmt.Println(err)
}
err = parseTemplate("templates/basic.tmpl", "index.html", content)
if err != nil {
fmt.Println(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment