package templating_test
import (
"context"
html "html/template"
"log"
"os"
text "text/template"
"gitoa.ru/go-4devs/templating"
"gitoa.ru/go-4devs/templating/gohtml"
"gitoa.ru/go-4devs/templating/gotxt"
"gitoa.ru/go-4devs/templating/render"
)
type Data struct {
Name string
}
func ExampleExecute() {
ctx := context.Background()
data := Data{Name: "andrey"}
gohtml.Must(html.New("example.html").Parse(`Hello {{ .Name }}!`))
err := templating.Execute(ctx, os.Stdout, "example.html.gohtml", data)
if err != nil {
log.Fatalln(err)
}
// Output:
// Hello andrey!
}
func ExampleExecute_withOption() {
ctx := context.Background()
data := Data{Name: "world"}
gohtml.Must(html.New("example_with_option.html").Parse(`Hello {{ .Name }}!`))
err := templating.Execute(ctx, os.Stdout, "example_with_option.html", data, render.WithEngine(gohtml.Name))
if err != nil {
log.Fatalln(err)
}
// Output:
// Hello <b>world</b>!
}
func ExampleExecute_text() {
ctx := context.Background()
data := Data{Name: "text"}
gotxt.Must(text.New("example.txt").Parse(`Hello {{ .Name }}!`))
err := templating.Execute(ctx, os.Stdout, "example.txt.gotxt", data)
if err != nil {
log.Fatalln(err)
}
// Output:
// Hello text!
}
func ExampleExecute_withoutEngine() {
ctx := context.Background()
data := Data{Name: "engine"}
gotxt.Must(text.New("example_engine.txt").Parse(`Text {{ .Name }}!`))
gohtml.Must(html.New("example_engine.html").Parse(`Html {{ .Name }}!`))
err := templating.Execute(ctx, os.Stdout, "example_engine.html", data)
if err != nil {
log.Fatalln(err)
}
// Output:
// Html engine!
}