You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
1.6 KiB

2 years ago
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: "<b>world</b>"}
gohtml.Must(html.New("example_with_option.html").Parse(`Hello <s>{{ .Name }}</s>!`))
err := templating.Execute(ctx, os.Stdout, "example_with_option.html", data, render.WithEngine(gohtml.Name))
if err != nil {
log.Fatalln(err)
}
// Output:
// Hello <s>&lt;b&gt;world&lt;/b&gt;</s>!
}
func ExampleExecute_text() {
ctx := context.Background()
data := Data{Name: "<u>text</u>"}
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 <u>text</u>!
}
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!
}