Created
September 9, 2014 16:35
-
-
Save hnakamur/f0ab38b76ae3385e2deb to your computer and use it in GitHub Desktop.
example using [pongo2](https://github.com/flosch/pongo2)
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
Hello, {{ name }} |
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 ( | |
"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