Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
张磊
/
FileStorageBeego
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
9e1d5036
authored
2013-08-10 16:33:46 +0800
by
miraclesu
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
Add renderform template function
1 parent
e47b2b67
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
93 additions
and
2 deletions
template.go
utils.go
utils_test.go
template.go
View file @
9e1d503
...
...
@@ -31,6 +31,7 @@ func init() {
beegoTplFuncMap
[
"str2html"
]
=
Str2html
beegoTplFuncMap
[
"htmlquote"
]
=
Htmlquote
beegoTplFuncMap
[
"htmlunquote"
]
=
Htmlunquote
beegoTplFuncMap
[
"renderform"
]
=
RenderForm
}
// AddFuncMap let user to register a func in the template
...
...
utils.go
View file @
9e1d503
...
...
@@ -178,7 +178,7 @@ func inSlice(v string, sl []string) bool {
func
ParseForm
(
form
url
.
Values
,
obj
interface
{})
error
{
objT
:=
reflect
.
TypeOf
(
obj
)
objV
:=
reflect
.
ValueOf
(
obj
)
if
!
(
objT
.
Kind
()
==
reflect
.
Ptr
&&
objT
.
Elem
()
.
Kind
()
==
reflect
.
Struct
)
{
if
!
isStructPtr
(
objT
)
{
return
fmt
.
Errorf
(
"%v must be a struct pointer"
,
obj
)
}
objT
=
objT
.
Elem
()
...
...
@@ -189,8 +189,8 @@ func ParseForm(form url.Values, obj interface{}) error {
if
!
fieldV
.
CanSet
()
{
continue
}
fieldT
:=
objT
.
Field
(
i
)
fieldT
:=
objT
.
Field
(
i
)
tags
:=
strings
.
Split
(
fieldT
.
Tag
.
Get
(
"form"
),
","
)
var
tag
string
if
len
(
tags
)
==
0
||
len
(
tags
[
0
])
==
0
{
...
...
@@ -238,6 +238,69 @@ func ParseForm(form url.Values, obj interface{}) error {
return
nil
}
// form types for RenderForm function
var
FormType
=
map
[
string
]
bool
{
"text"
:
true
,
"textarea"
:
true
,
"hidden"
:
true
,
"password"
:
true
,
}
var
unKind
=
map
[
reflect
.
Kind
]
bool
{
reflect
.
Uintptr
:
true
,
reflect
.
Complex64
:
true
,
reflect
.
Complex128
:
true
,
reflect
.
Array
:
true
,
reflect
.
Chan
:
true
,
reflect
.
Func
:
true
,
reflect
.
Map
:
true
,
reflect
.
Ptr
:
true
,
reflect
.
Slice
:
true
,
reflect
.
Struct
:
true
,
reflect
.
UnsafePointer
:
true
,
}
// obj must be a struct pointer
func
RenderForm
(
obj
interface
{})
template
.
HTML
{
objT
:=
reflect
.
TypeOf
(
obj
)
objV
:=
reflect
.
ValueOf
(
obj
)
if
!
isStructPtr
(
objT
)
{
return
template
.
HTML
(
""
)
}
objT
=
objT
.
Elem
()
objV
=
objV
.
Elem
()
var
raw
[]
string
for
i
:=
0
;
i
<
objT
.
NumField
();
i
++
{
fieldV
:=
objV
.
Field
(
i
)
if
!
fieldV
.
CanSet
()
||
unKind
[
fieldV
.
Kind
()]
{
continue
}
fieldT
:=
objT
.
Field
(
i
)
tags
:=
strings
.
Split
(
fieldT
.
Tag
.
Get
(
"form"
),
","
)
name
:=
fieldT
.
Name
if
len
(
tags
)
<
2
{
if
len
(
tags
)
==
1
&&
len
(
tags
[
0
])
>
0
{
name
=
tags
[
0
]
}
raw
=
append
(
raw
,
fmt
.
Sprintf
(
`%v: <input name="%v" type="text" value="%v">`
,
fieldT
.
Name
,
name
,
fieldV
.
Interface
()))
}
else
{
if
len
(
tags
[
0
])
>
0
{
name
=
tags
[
0
]
}
raw
=
append
(
raw
,
fmt
.
Sprintf
(
`%v: <input name="%v" type="%v" value="%v">`
,
fieldT
.
Name
,
name
,
tags
[
1
],
fieldV
.
Interface
()))
}
}
return
template
.
HTML
(
strings
.
Join
(
raw
,
"</br>"
))
}
func
isStructPtr
(
t
reflect
.
Type
)
bool
{
return
t
.
Kind
()
==
reflect
.
Ptr
&&
t
.
Elem
()
.
Kind
()
==
reflect
.
Struct
}
func
stringsToJson
(
str
string
)
string
{
rs
:=
[]
rune
(
str
)
jsons
:=
""
...
...
utils_test.go
View file @
9e1d503
package
beego
import
(
"html/template"
"net/url"
"testing"
"time"
...
...
@@ -144,3 +145,29 @@ func TestParseForm(t *testing.T) {
t
.
Errorf
(
"Intro should equal `I am an engineer!` but got `%v`"
,
u
.
Intro
)
}
}
func
TestRenderForm
(
t
*
testing
.
T
)
{
type
user
struct
{
Id
int
tag
string
`form:tag`
Name
interface
{}
`form:"username"`
Age
int
`form:"age,text"`
Email
[]
string
Intro
string
`form:",textarea"`
}
u
:=
user
{
Name
:
"test"
}
output
:=
RenderForm
(
u
)
if
output
!=
template
.
HTML
(
""
)
{
t
.
Errorf
(
"output should be empty but got %v"
,
output
)
}
output
=
RenderForm
(
&
u
)
result
:=
template
.
HTML
(
`Id: <input name="Id" type="text" value="0"></br>`
+
`Name: <input name="username" type="text" value="test"></br>`
+
`Age: <input name="age" type="text" value="0"></br>`
+
`Intro: <input name="Intro" type="textarea" value="">`
)
if
output
!=
result
{
t
.
Errorf
(
"output should equal `%v` but got `%v`"
,
result
,
output
)
}
}
...
...
Write
Preview
Styling with
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment