diff options
-rw-r--r-- | cmd/ponzu/contentType.tmpl | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/cmd/ponzu/contentType.tmpl b/cmd/ponzu/contentType.tmpl new file mode 100644 index 0000000..a631fe7 --- /dev/null +++ b/cmd/ponzu/contentType.tmpl @@ -0,0 +1,56 @@ +package content + +import ( + "fmt" + + "github.com/bosssauce/ponzu/management/editor" +) + + +type {{ .Name }} struct { + Item + editor editor.Editor + + {{ range .Fields }}{{ .Name }} {{ .TypeName }} `json:"{{ .JSONName }}"` + {{ end }} +} + +// MarshalEditor writes a buffer of html to edit a {{ .Name }} +// partially implements editor.Editable +func ({{ .Initial }} *{{ .Name }}) MarshalEditor() ([]byte, error) { + view, err := editor.Form({{ .Initial }}, + // Take note that the first argument to these Input-like functions + // is the string version of each {{ .Name }} field, and must follow + // this pattern for auto-decoding and auto-encoding reasons: + + {{ $initial := .Initial }} + {{ range .Fields }} + editor.Field{ + View: editor.Input("{{ .Name }}", {{ $initial }}, map[string]string{ + "label": "{{ .Name }}", + "type": "text", + "placeholder": "Enter the {{ .Name }} here", + }), + }, + {{ end }} + ) + + if err != nil { + return nil, fmt.Errorf("Failed to render {{ .Name }} editor view: %s", err.Error()) + } + + return view, nil +} + +func init() { + Types["{{ .Name }}"] = func() interface{} { return new({{ .Name }}) } +} + +// ContentName is required to set the display name for a piece of content in the editor +// Partially implements editor.Editable +func ({{ .Initial }} *{{ .Name }}) ContentName() string { return {{ .Name }}-{{ .Initial }}.ItemID() } + +// Editor is a buffer of bytes for the Form function to write input views +// partially implements editor.Editable +func ({{ .Initial }} *{{ .Name }}) Editor() *editor.Editor { return &{{ .Initial }}.editor } + |