Skip to content

Commit

Permalink
improved
Browse files Browse the repository at this point in the history
  • Loading branch information
admpub committed Oct 17, 2014
1 parent 4840829 commit 37c9725
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
9 changes: 4 additions & 5 deletions fields/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type FieldInterface interface {
SetChoices(choices interface{}, saveIndex ...bool) FieldInterface
SetText(text string) FieldInterface
SetData(key string, value interface{})
Data() map[string]interface{}
String() string
}

Expand Down Expand Up @@ -119,7 +120,7 @@ func (f *Field) Name() string {
return strings.TrimSuffix(f.name, "[]")
}

func (f *Field) dataForRender() map[string]interface{} {
func (f *Field) Data() map[string]interface{} {
safeParams := make(map[template.HTMLAttr]interface{})
for k, v := range f.params {
safeParams[template.HTMLAttr(k)] = v
Expand Down Expand Up @@ -152,16 +153,14 @@ func (f *Field) dataForRender() map[string]interface{} {
// Render packs all data and executes widget render method.
func (f *Field) Render() template.HTML {
if f.Widget != nil {
data := f.dataForRender()
return template.HTML(f.Widget.Render(data))
return template.HTML(f.Widget.Render(f.Data()))
}
return template.HTML("")
}

func (f *Field) String() string {
if f.Widget != nil {
data := f.dataForRender()
return f.Widget.Render(data)
return f.Widget.Render(f.Data())
}
return ""
}
Expand Down
11 changes: 7 additions & 4 deletions fieldset.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ func (f *FieldSetType) SetData(key string, value interface{}) {
f.AppendData[key] = value
}

func (f *FieldSetType) dataForRender() string {
var s string
buf := bytes.NewBufferString(s)
func (f *FieldSetType) Data() map[string]interface{} {
data := map[string]interface{}{
"container": "fieldset",
"name": f.name,
Expand All @@ -39,13 +37,18 @@ func (f *FieldSetType) dataForRender() string {
for k, v := range f.AppendData {
data[k] = v
}
return data
}

func (f *FieldSetType) dataForRender() string {
buf := bytes.NewBufferString("")
tpf := formcommon.TmplDir + "/" + f.tmpl + ".html"
tpl, ok := formcommon.CachedTemplate(tpf)
if !ok {
tpl = template.Must(template.ParseFiles(formcommon.CreateUrl(tpf)))
formcommon.SetCachedTemplate(tpf, tpl)
}
err := tpl.Execute(buf, data)
err := tpl.Execute(buf, f.Data())
if err != nil {
panic(err)
}
Expand Down
17 changes: 13 additions & 4 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@ type FormElement interface {
Name() string
String() string
SetData(key string, value interface{})
Data() map[string]interface{}
}

func (f *Form) SetData(key string, value interface{}) {
f.AppendData[key] = value
}

func (f *Form) dataForRender() string {
var s string
buf := bytes.NewBufferString(s)
func (f *Form) Data() map[string]interface{} {
data := map[string]interface{}{
"container": "",
"fields": f.fields,
Expand All @@ -39,7 +38,12 @@ func (f *Form) dataForRender() string {
for k, v := range f.AppendData {
data[k] = v
}
err := f.template.Execute(buf, data)
return data
}

func (f *Form) dataForRender() string {
buf := bytes.NewBufferString("")
err := f.template.Execute(buf, f.Data())
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -167,6 +171,11 @@ func (f *Form) Field(name string) fields.FieldInterface {
return f.fields[ind].(fields.FieldInterface)
}

// Fields returns all field
func (f *Form) Fields() []FormElement {
return f.fields
}

// Field returns the field identified by name. It returns an empty field if it is missing.
func (f *Form) FieldSet(name string) *FieldSetType {
ind, ok := f.fieldMap[name]
Expand Down

0 comments on commit 37c9725

Please sign in to comment.