Files
ent/doc/md/templates.md
2020-05-09 23:42:00 +03:00

1.7 KiB

id, title
id title
templates External Templates

entc accepts external Go templates to execute using the --template flag. If the template name is already defined by entc, it will override the existing one. Otherwise, it will write the execution output to a file with the same name as the template. For example:

node.tmpl - This template example will be written in a file named: ent/node.go.

{{ define "node" }}

{{/* Add the base header for the generated file */}}
{{ $pkg := base $.Config.Package }}
{{ template "header" $ }}

{{/* Loop over all nodes and add implement the "GoStringer" interface */}}
{{ range $n := $.Nodes }}
	{{ $receiver := $n.Receiver }}
	func ({{ $receiver }} *{{ $n.Name }}) GoString() string {
		if {{ $receiver }} == nil {
			return fmt.Sprintf("{{ $n.Name }}(nil)")
		}
		return {{ $receiver }}.String()
	}
{{ end }}

{{ end }}

In order to override an existing template, use its name. For example:

{{/* A template for adding additional fields to specific types. */}}
{{ define "model/fields/additional" }}
	{{- /* Add static fields to the "Card" entity. */}}
	{{- if eq $.Name "Card" }}
		// StaticField defined by templates.
		StaticField string `json:"static_field,omitempty"`
	{{- end }}
{{ end }}

Examples

A custom template for implementing the Node API for GraphQL - Github.

Documentation

Templates are executed on either a specific node-type or the entire schema graph. For API documentation, see the GoDoc.