Skip to content

Instantly share code, notes, and snippets.

@beta0x64 beta0x64/template.go
Created Sep 30, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.