Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
c6167ef1
Commit
c6167ef1
authored
Oct 30, 2013
by
astaxie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix #260
parent
4bcbc588
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
67 additions
and
19 deletions
+67
-19
template.go
template.go
+14
-19
template_test.go
template_test.go
+53
-0
No files found.
template.go
View file @
c6167ef1
...
@@ -133,8 +133,16 @@ func BuildTemplate(dir string) error {
...
@@ -133,8 +133,16 @@ func BuildTemplate(dir string) error {
return
nil
return
nil
}
}
func
getTplDeep
(
root
,
file
string
,
t
*
template
.
Template
)
(
*
template
.
Template
,
[][]
string
,
error
)
{
func
getTplDeep
(
root
,
file
,
parent
string
,
t
*
template
.
Template
)
(
*
template
.
Template
,
[][]
string
,
error
)
{
fileabspath
:=
filepath
.
Join
(
root
,
file
)
var
fileabspath
string
if
filepath
.
HasPrefix
(
file
,
"../"
)
{
fileabspath
=
filepath
.
Join
(
root
,
filepath
.
Dir
(
parent
),
file
)
}
else
{
fileabspath
=
filepath
.
Join
(
root
,
file
)
}
if
e
,
_
:=
FileExists
(
fileabspath
);
!
e
{
panic
(
"can't find template file"
+
file
)
}
data
,
err
:=
ioutil
.
ReadFile
(
fileabspath
)
data
,
err
:=
ioutil
.
ReadFile
(
fileabspath
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
[][]
string
{},
err
return
nil
,
[][]
string
{},
err
...
@@ -154,23 +162,10 @@ func getTplDeep(root, file string, t *template.Template) (*template.Template, []
...
@@ -154,23 +162,10 @@ func getTplDeep(root, file string, t *template.Template) (*template.Template, []
if
!
HasTemplateEXt
(
m
[
1
])
{
if
!
HasTemplateEXt
(
m
[
1
])
{
continue
continue
}
}
if
e
,
_
:=
FileExists
(
filepath
.
Join
(
root
,
m
[
1
]));
e
{
t
,
_
,
err
=
getTplDeep
(
root
,
m
[
1
],
file
,
t
)
t
,
_
,
err
=
getTplDeep
(
root
,
m
[
1
],
t
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
[][]
string
{},
err
return
nil
,
[][]
string
{},
err
}
}
}
else
{
relativefile
:=
filepath
.
Join
(
filepath
.
Dir
(
file
),
m
[
1
])
if
e
,
_
:=
FileExists
(
relativefile
);
e
{
t
,
_
,
err
=
getTplDeep
(
root
,
relativefile
,
t
)
if
err
!=
nil
{
return
nil
,
[][]
string
{},
err
}
}
else
{
panic
(
"can't find template file"
+
m
[
1
])
}
}
}
}
}
}
return
t
,
allsub
,
nil
return
t
,
allsub
,
nil
...
@@ -179,7 +174,7 @@ func getTplDeep(root, file string, t *template.Template) (*template.Template, []
...
@@ -179,7 +174,7 @@ func getTplDeep(root, file string, t *template.Template) (*template.Template, []
func
getTemplate
(
root
,
file
string
,
others
...
string
)
(
t
*
template
.
Template
,
err
error
)
{
func
getTemplate
(
root
,
file
string
,
others
...
string
)
(
t
*
template
.
Template
,
err
error
)
{
t
=
template
.
New
(
file
)
.
Delims
(
TemplateLeft
,
TemplateRight
)
.
Funcs
(
beegoTplFuncMap
)
t
=
template
.
New
(
file
)
.
Delims
(
TemplateLeft
,
TemplateRight
)
.
Funcs
(
beegoTplFuncMap
)
var
submods
[][]
string
var
submods
[][]
string
t
,
submods
,
err
=
getTplDeep
(
root
,
file
,
t
)
t
,
submods
,
err
=
getTplDeep
(
root
,
file
,
""
,
t
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
...
@@ -203,7 +198,7 @@ func _getTemplate(t0 *template.Template, root string, submods [][]string, others
...
@@ -203,7 +198,7 @@ func _getTemplate(t0 *template.Template, root string, submods [][]string, others
for
_
,
otherfile
:=
range
others
{
for
_
,
otherfile
:=
range
others
{
if
otherfile
==
m
[
1
]
{
if
otherfile
==
m
[
1
]
{
var
submods1
[][]
string
var
submods1
[][]
string
t
,
submods1
,
err
=
getTplDeep
(
root
,
otherfile
,
t
)
t
,
submods1
,
err
=
getTplDeep
(
root
,
otherfile
,
""
,
t
)
if
err
!=
nil
{
if
err
!=
nil
{
Trace
(
"template parse file err:"
,
err
)
Trace
(
"template parse file err:"
,
err
)
}
else
if
submods1
!=
nil
&&
len
(
submods1
)
>
0
{
}
else
if
submods1
!=
nil
&&
len
(
submods1
)
>
0
{
...
@@ -224,7 +219,7 @@ func _getTemplate(t0 *template.Template, root string, submods [][]string, others
...
@@ -224,7 +219,7 @@ func _getTemplate(t0 *template.Template, root string, submods [][]string, others
for
_
,
sub
:=
range
allsub
{
for
_
,
sub
:=
range
allsub
{
if
len
(
sub
)
==
2
&&
sub
[
1
]
==
m
[
1
]
{
if
len
(
sub
)
==
2
&&
sub
[
1
]
==
m
[
1
]
{
var
submods1
[][]
string
var
submods1
[][]
string
t
,
submods1
,
err
=
getTplDeep
(
root
,
otherfile
,
t
)
t
,
submods1
,
err
=
getTplDeep
(
root
,
otherfile
,
""
,
t
)
if
err
!=
nil
{
if
err
!=
nil
{
Trace
(
"template parse file err:"
,
err
)
Trace
(
"template parse file err:"
,
err
)
}
else
if
submods1
!=
nil
&&
len
(
submods1
)
>
0
{
}
else
if
submods1
!=
nil
&&
len
(
submods1
)
>
0
{
...
...
template_test.go
View file @
c6167ef1
...
@@ -67,3 +67,56 @@ func TestTemplate(t *testing.T) {
...
@@ -67,3 +67,56 @@ func TestTemplate(t *testing.T) {
}
}
os
.
RemoveAll
(
dir
)
os
.
RemoveAll
(
dir
)
}
}
var
menu
string
=
`<div class="menu">
<ul>
<li>menu1</li>
<li>menu2</li>
<li>menu3</li>
</ul>
</div>
`
var
user
string
=
`<!DOCTYPE html>
<html>
<head>
<title>beego welcome template</title>
</head>
<body>
{{template "../public/menu.tpl"}}
</body>
</html>
`
func
TestRelativeTemplate
(
t
*
testing
.
T
)
{
dir
:=
"_beeTmp"
files
:=
[]
string
{
"easyui/public/menu.tpl"
,
"easyui/rbac/user.tpl"
,
}
if
err
:=
os
.
MkdirAll
(
dir
,
0777
);
err
!=
nil
{
t
.
Fatal
(
err
)
}
for
k
,
name
:=
range
files
{
os
.
MkdirAll
(
filepath
.
Dir
(
filepath
.
Join
(
dir
,
name
)),
0777
)
if
f
,
err
:=
os
.
Create
(
filepath
.
Join
(
dir
,
name
));
err
!=
nil
{
t
.
Fatal
(
err
)
}
else
{
if
k
==
0
{
f
.
WriteString
(
menu
)
}
else
if
k
==
1
{
f
.
WriteString
(
user
)
}
f
.
Close
()
}
}
if
err
:=
BuildTemplate
(
dir
);
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
err
:=
BeeTemplates
[
"easyui/rbac/user.tpl"
]
.
ExecuteTemplate
(
os
.
Stdout
,
"easyui/rbac/user.tpl"
,
nil
);
err
!=
nil
{
t
.
Fatal
(
err
)
}
for
_
,
name
:=
range
files
{
os
.
RemoveAll
(
filepath
.
Join
(
dir
,
name
))
}
os
.
RemoveAll
(
dir
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
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 comment