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
3da28535
Commit
3da28535
authored
Feb 25, 2016
by
JessonChan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
lock the templates map when goroutie update the map
parent
2b23764e
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
49 additions
and
49 deletions
+49
-49
template.go
template.go
+49
-49
No files found.
template.go
View file @
3da28535
...
...
@@ -23,6 +23,7 @@ import (
"path/filepath"
"regexp"
"strings"
"sync"
"github.com/astaxie/beego/utils"
)
...
...
@@ -30,7 +31,8 @@ import (
var
(
beegoTplFuncMap
=
make
(
template
.
FuncMap
)
// BeeTemplates caching map and supported template file extensions.
BeeTemplates
=
make
(
map
[
string
]
*
template
.
Template
)
BeeTemplates
=
make
(
map
[
string
]
*
template
.
Template
)
templatesLock
sync
.
Mutex
// BeeTemplateExt stores the template extension which will build
BeeTemplateExt
=
[]
string
{
"tpl"
,
"html"
}
)
...
...
@@ -66,17 +68,21 @@ func init() {
}
// AddFuncMap let user to register a func in the template.
func
AddFuncMap
(
key
string
,
f
unname
interface
{})
error
{
beegoTplFuncMap
[
key
]
=
f
unname
func
AddFuncMap
(
key
string
,
f
n
interface
{})
error
{
beegoTplFuncMap
[
key
]
=
f
n
return
nil
}
type
template
f
ile
struct
{
type
template
F
ile
struct
{
root
string
files
map
[
string
][]
string
}
func
(
tf
*
templatefile
)
visit
(
paths
string
,
f
os
.
FileInfo
,
err
error
)
error
{
// visit will make the paths into two part,the first is subDir (without tf.root),the second is full path(without tf.root).
// if tf.root="views" and
// paths is "views/errors/404.html",the subDir will be "errors",the file will be "errors/404.html"
// paths is "views/admin/errors/404.html",the subDir will be "admin/errors",the file will be "admin/errors/404.html"
func
(
tf
*
templateFile
)
visit
(
paths
string
,
f
os
.
FileInfo
,
err
error
)
error
{
if
f
==
nil
{
return
err
}
...
...
@@ -88,18 +94,10 @@ func (tf *templatefile) visit(paths string, f os.FileInfo, err error) error {
}
replace
:=
strings
.
NewReplacer
(
"
\\
"
,
"/"
)
a
:=
[]
byte
(
paths
)
a
=
a
[
len
([]
byte
(
tf
.
root
))
:
]
file
:=
strings
.
TrimLeft
(
replace
.
Replace
(
string
(
a
)),
"/"
)
subdir
:=
filepath
.
Dir
(
file
)
if
_
,
ok
:=
tf
.
files
[
subdir
];
ok
{
tf
.
files
[
subdir
]
=
append
(
tf
.
files
[
subdir
],
file
)
}
else
{
m
:=
make
([]
string
,
1
)
m
[
0
]
=
file
tf
.
files
[
subdir
]
=
m
}
file
:=
strings
.
TrimLeft
(
replace
.
Replace
(
paths
[
len
(
tf
.
root
)
:
]),
"/"
)
subDir
:=
filepath
.
Dir
(
file
)
tf
.
files
[
subDir
]
=
append
(
tf
.
files
[
subDir
],
file
)
return
nil
}
...
...
@@ -132,7 +130,7 @@ func BuildTemplate(dir string, files ...string) error {
}
return
errors
.
New
(
"dir open err"
)
}
self
:=
&
template
f
ile
{
self
:=
&
template
F
ile
{
root
:
dir
,
files
:
make
(
map
[
string
][]
string
),
}
...
...
@@ -146,12 +144,14 @@ func BuildTemplate(dir string, files ...string) error {
for
_
,
v
:=
range
self
.
files
{
for
_
,
file
:=
range
v
{
if
len
(
files
)
==
0
||
utils
.
InSlice
(
file
,
files
)
{
templatesLock
.
Lock
()
t
,
err
:=
getTemplate
(
self
.
root
,
file
,
v
...
)
if
err
!=
nil
{
Trace
(
"parse template err:"
,
file
,
err
)
}
else
{
BeeTemplates
[
file
]
=
t
}
templatesLock
.
Unlock
()
}
}
}
...
...
@@ -159,16 +159,16 @@ func BuildTemplate(dir string, files ...string) error {
}
func
getTplDeep
(
root
,
file
,
parent
string
,
t
*
template
.
Template
)
(
*
template
.
Template
,
[][]
string
,
error
)
{
var
file
absp
ath
string
var
file
AbsP
ath
string
if
filepath
.
HasPrefix
(
file
,
"../"
)
{
file
absp
ath
=
filepath
.
Join
(
root
,
filepath
.
Dir
(
parent
),
file
)
file
AbsP
ath
=
filepath
.
Join
(
root
,
filepath
.
Dir
(
parent
),
file
)
}
else
{
file
absp
ath
=
filepath
.
Join
(
root
,
file
)
file
AbsP
ath
=
filepath
.
Join
(
root
,
file
)
}
if
e
:=
utils
.
FileExists
(
file
absp
ath
);
!
e
{
if
e
:=
utils
.
FileExists
(
file
AbsP
ath
);
!
e
{
panic
(
"can't find template file:"
+
file
)
}
data
,
err
:=
ioutil
.
ReadFile
(
file
absp
ath
)
data
,
err
:=
ioutil
.
ReadFile
(
file
AbsP
ath
)
if
err
!=
nil
{
return
nil
,
[][]
string
{},
err
}
...
...
@@ -177,11 +177,11 @@ func getTplDeep(root, file, parent string, t *template.Template) (*template.Temp
return
nil
,
[][]
string
{},
err
}
reg
:=
regexp
.
MustCompile
(
BConfig
.
WebConfig
.
TemplateLeft
+
"[ ]*template[ ]+
\"
([^
\"
]+)
\"
"
)
all
s
ub
:=
reg
.
FindAllStringSubmatch
(
string
(
data
),
-
1
)
for
_
,
m
:=
range
all
s
ub
{
all
S
ub
:=
reg
.
FindAllStringSubmatch
(
string
(
data
),
-
1
)
for
_
,
m
:=
range
all
S
ub
{
if
len
(
m
)
==
2
{
tl
ook
:=
t
.
Lookup
(
m
[
1
])
if
tl
ook
!=
nil
{
tl
:=
t
.
Lookup
(
m
[
1
])
if
tl
!=
nil
{
continue
}
if
!
HasTemplateExt
(
m
[
1
])
{
...
...
@@ -193,17 +193,17 @@ func getTplDeep(root, file, parent string, t *template.Template) (*template.Temp
}
}
}
return
t
,
all
s
ub
,
nil
return
t
,
all
S
ub
,
nil
}
func
getTemplate
(
root
,
file
string
,
others
...
string
)
(
t
*
template
.
Template
,
err
error
)
{
t
=
template
.
New
(
file
)
.
Delims
(
BConfig
.
WebConfig
.
TemplateLeft
,
BConfig
.
WebConfig
.
TemplateRight
)
.
Funcs
(
beegoTplFuncMap
)
var
sub
m
ods
[][]
string
t
,
sub
m
ods
,
err
=
getTplDeep
(
root
,
file
,
""
,
t
)
var
sub
M
ods
[][]
string
t
,
sub
M
ods
,
err
=
getTplDeep
(
root
,
file
,
""
,
t
)
if
err
!=
nil
{
return
nil
,
err
}
t
,
err
=
_getTemplate
(
t
,
root
,
sub
m
ods
,
others
...
)
t
,
err
=
_getTemplate
(
t
,
root
,
sub
M
ods
,
others
...
)
if
err
!=
nil
{
return
nil
,
err
...
...
@@ -211,44 +211,44 @@ func getTemplate(root, file string, others ...string) (t *template.Template, err
return
}
func
_getTemplate
(
t0
*
template
.
Template
,
root
string
,
sub
m
ods
[][]
string
,
others
...
string
)
(
t
*
template
.
Template
,
err
error
)
{
func
_getTemplate
(
t0
*
template
.
Template
,
root
string
,
sub
M
ods
[][]
string
,
others
...
string
)
(
t
*
template
.
Template
,
err
error
)
{
t
=
t0
for
_
,
m
:=
range
sub
m
ods
{
for
_
,
m
:=
range
sub
M
ods
{
if
len
(
m
)
==
2
{
t
em
pl
:=
t
.
Lookup
(
m
[
1
])
if
t
em
pl
!=
nil
{
tpl
:=
t
.
Lookup
(
m
[
1
])
if
tpl
!=
nil
{
continue
}
//first check filename
for
_
,
other
f
ile
:=
range
others
{
if
other
f
ile
==
m
[
1
]
{
var
sub
m
ods1
[][]
string
t
,
sub
mods1
,
err
=
getTplDeep
(
root
,
otherf
ile
,
""
,
t
)
for
_
,
other
F
ile
:=
range
others
{
if
other
F
ile
==
m
[
1
]
{
var
sub
M
ods1
[][]
string
t
,
sub
Mods1
,
err
=
getTplDeep
(
root
,
otherF
ile
,
""
,
t
)
if
err
!=
nil
{
Trace
(
"template parse file err:"
,
err
)
}
else
if
sub
mods1
!=
nil
&&
len
(
subm
ods1
)
>
0
{
t
,
err
=
_getTemplate
(
t
,
root
,
sub
m
ods1
,
others
...
)
}
else
if
sub
Mods1
!=
nil
&&
len
(
subM
ods1
)
>
0
{
t
,
err
=
_getTemplate
(
t
,
root
,
sub
M
ods1
,
others
...
)
}
break
}
}
//second check define
for
_
,
other
f
ile
:=
range
others
{
file
abspath
:=
filepath
.
Join
(
root
,
otherf
ile
)
data
,
err
:=
ioutil
.
ReadFile
(
file
absp
ath
)
for
_
,
other
F
ile
:=
range
others
{
file
AbsPath
:=
filepath
.
Join
(
root
,
otherF
ile
)
data
,
err
:=
ioutil
.
ReadFile
(
file
AbsP
ath
)
if
err
!=
nil
{
continue
}
reg
:=
regexp
.
MustCompile
(
BConfig
.
WebConfig
.
TemplateLeft
+
"[ ]*define[ ]+
\"
([^
\"
]+)
\"
"
)
all
s
ub
:=
reg
.
FindAllStringSubmatch
(
string
(
data
),
-
1
)
for
_
,
sub
:=
range
all
s
ub
{
all
S
ub
:=
reg
.
FindAllStringSubmatch
(
string
(
data
),
-
1
)
for
_
,
sub
:=
range
all
S
ub
{
if
len
(
sub
)
==
2
&&
sub
[
1
]
==
m
[
1
]
{
var
sub
m
ods1
[][]
string
t
,
sub
mods1
,
err
=
getTplDeep
(
root
,
otherf
ile
,
""
,
t
)
var
sub
M
ods1
[][]
string
t
,
sub
Mods1
,
err
=
getTplDeep
(
root
,
otherF
ile
,
""
,
t
)
if
err
!=
nil
{
Trace
(
"template parse file err:"
,
err
)
}
else
if
sub
mods1
!=
nil
&&
len
(
subm
ods1
)
>
0
{
t
,
err
=
_getTemplate
(
t
,
root
,
sub
m
ods1
,
others
...
)
}
else
if
sub
Mods1
!=
nil
&&
len
(
subM
ods1
)
>
0
{
t
,
err
=
_getTemplate
(
t
,
root
,
sub
M
ods1
,
others
...
)
}
break
}
...
...
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