Skip to content

Instantly share code, notes, and snippets.

@hnakamur
Created September 9, 2014 16:35
Show Gist options
  • Save hnakamur/f0ab38b76ae3385e2deb to your computer and use it in GitHub Desktop.
Save hnakamur/f0ab38b76ae3385e2deb to your computer and use it in GitHub Desktop.
example using [pongo2](https://github.com/flosch/pongo2)
Hello, {{ name }}
package main
import (
"bufio"
"os"
"github.com/flosch/pongo2"
)
var exampleTpl = pongo2.Must(pongo2.FromFile("example.j2"))
func main() {
outFilename := "example.txt"
ctx := pongo2.Context{
"name": "John",
}
err := executeTemplateToFile(exampleTpl, ctx, outFilename)
if err != nil {
panic(err)
}
}
func executeTemplateToFile(tpl *pongo2.Template, ctx pongo2.Context, fn string) error {
file, err := os.Create(fn)
if err != nil {
return err
}
defer file.Close()
writer := bufio.NewWriter(file)
err = tpl.ExecuteWriter(ctx, writer)
if err != nil {
return err
}
return writer.Flush()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment