-
Notifications
You must be signed in to change notification settings - Fork 9
How to use easygen API
suntong edited this page Aug 5, 2020
·
5 revisions
easygen's API has been the same since V2. I.e., use easygen.NewTemplate()
to init a new template, with the built-in base template functions:
tmpl := easygen.NewTemplate().Funcs(easygen.FuncDefs())
then use easygen.Execute()
:
err := easygen.Execute(tmpl, os.Stdout, templateFile, settingVar)
Or, to get the result into variable, use the following:
buf := new(bytes.Buffer)
err := easygen.Execute(tmpl, buf, templateFile, settingVar)
check(err)
return buf.String()
To directly use template string instead of template file, use the following:
buf := new(bytes.Buffer)
err := easygen.Process0(tmpl, buf, templateStr, yamlFileName)
To directly use template string on variable (without an input data file), use the following:
var b strings.Builder
err := easygen.Execute0(tmpl, &b, templateStr, dataOfAnyType)
See the Example under Execute0()
godoc for full code.
Finally, to use easygen.NewTemplate()
to init a new template, with all the easygen
provided template functions:
tmpl0 := easygen.NewTemplate().Customize()
tmpl := tmpl0.Funcs(easygen.FuncDefs()).Funcs(egVar.FuncDefs()).Funcs(egCal.FuncDefs()).Funcs(egFilePath.FuncDefs())
See the Example under Execute()
godoc for full code.
See also,