doc: update examples for custom templates

This commit is contained in:
Ariel Mashraki
2020-09-15 15:30:11 +03:00
committed by Ariel Mashraki
parent e26e99b643
commit c6f9d25d8e
2 changed files with 49 additions and 7 deletions

View File

@@ -12,23 +12,35 @@ package main
import (
"log"
"strings"
"text/template"
"github.com/facebook/ent/entc"
"github.com/facebook/ent/entc/gen"
"github.com/facebook/ent/schema/field"
)
func main() {
// A usage for custom templates with external functions.
// One template is defined in the option below, and the
// second template is provided with the `Templates` field.
opts := []entc.Option{
entc.Funcs(template.FuncMap{"title": strings.ToTitle}),
entc.TemplateFiles("template/static.tmpl"),
}
err := entc.Generate("./schema", &gen.Config{
Header: `
// Copyright 2019-present Facebook Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
// Code generated (@generated) by entc, DO NOT EDIT.
// Code generated by entc, DO NOT EDIT.
`,
IDType: &field.TypeInfo{Type: field.TypeInt},
})
Templates: []*template.Template{
template.Must(template.New("debug").
Funcs(gen.Funcs).
ParseFiles("template/debug.tmpl")),
},
}, opts...)
if err != nil {
log.Fatal("running ent codegen:", err)
}