Created
September 30, 2015 04:34
-
-
Save 0x1F602/c7f634a164e5a6180270 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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