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
1f46c1d2
Commit
1f46c1d2
authored
Mar 04, 2016
by
JessonChan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add template read lock when dev mode
parent
4b99e418
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
39 additions
and
29 deletions
+39
-29
controller.go
controller.go
+18
-15
template.go
template.go
+17
-10
template_test.go
template_test.go
+4
-4
No files found.
controller.go
View file @
1f46c1d2
...
...
@@ -199,6 +199,7 @@ func (c *Controller) RenderString() (string, error) {
func
(
c
*
Controller
)
RenderBytes
()
([]
byte
,
error
)
{
//if the controller has set layout, then first get the tplname's content set the content to the layout
var
buf
bytes
.
Buffer
var
err
error
if
c
.
Layout
!=
""
{
if
c
.
TplName
==
""
{
c
.
TplName
=
strings
.
ToLower
(
c
.
controllerName
)
+
"/"
+
strings
.
ToLower
(
c
.
actionName
)
+
"."
+
c
.
TplExt
...
...
@@ -216,13 +217,14 @@ func (c *Controller) RenderBytes() ([]byte, error) {
}
BuildTemplate
(
BConfig
.
WebConfig
.
ViewsPath
,
buildFiles
...
)
}
if
_
,
ok
:=
BeeTemplates
[
c
.
TplName
];
!
ok
{
if
t
:=
getTemplateByName
(
c
.
TplName
);
t
==
nil
{
panic
(
"can't find templatefile in the path:"
+
c
.
TplName
)
}
err
:=
BeeTemplates
[
c
.
TplName
]
.
ExecuteTemplate
(
&
buf
,
c
.
TplName
,
c
.
Data
)
if
err
!=
nil
{
Trace
(
"template Execute err:"
,
err
)
return
nil
,
err
}
else
{
err
=
t
.
ExecuteTemplate
(
&
buf
,
c
.
TplName
,
c
.
Data
)
if
err
!=
nil
{
Trace
(
"template Execute err:"
,
err
)
return
nil
,
err
}
}
c
.
Data
[
"LayoutContent"
]
=
template
.
HTML
(
buf
.
String
())
...
...
@@ -234,7 +236,7 @@ func (c *Controller) RenderBytes() ([]byte, error) {
}
buf
.
Reset
()
err
=
BeeTemplates
[
sectionTpl
]
.
ExecuteTemplate
(
&
buf
,
sectionTpl
,
c
.
Data
)
err
=
getTemplateByName
(
sectionTpl
)
.
ExecuteTemplate
(
&
buf
,
sectionTpl
,
c
.
Data
)
if
err
!=
nil
{
Trace
(
"template Execute err:"
,
err
)
return
nil
,
err
...
...
@@ -244,7 +246,7 @@ func (c *Controller) RenderBytes() ([]byte, error) {
}
buf
.
Reset
()
err
=
BeeTemplates
[
c
.
Layout
]
.
ExecuteTemplate
(
&
buf
,
c
.
Layout
,
c
.
Data
)
err
=
getTemplateByName
(
c
.
Layout
)
.
ExecuteTemplate
(
&
buf
,
c
.
Layout
,
c
.
Data
)
if
err
!=
nil
{
Trace
(
"template Execute err:"
,
err
)
return
nil
,
err
...
...
@@ -258,14 +260,15 @@ func (c *Controller) RenderBytes() ([]byte, error) {
if
BConfig
.
RunMode
==
DEV
{
BuildTemplate
(
BConfig
.
WebConfig
.
ViewsPath
,
c
.
TplName
)
}
if
_
,
ok
:=
BeeTemplates
[
c
.
TplName
];
!
ok
{
if
t
:=
getTemplateByName
(
c
.
TplName
);
t
==
nil
{
panic
(
"can't find templatefile in the path:"
+
c
.
TplName
)
}
buf
.
Reset
()
err
:=
BeeTemplates
[
c
.
TplName
]
.
ExecuteTemplate
(
&
buf
,
c
.
TplName
,
c
.
Data
)
if
err
!=
nil
{
Trace
(
"template Execute err:"
,
err
)
return
nil
,
err
}
else
{
buf
.
Reset
()
err
=
t
.
ExecuteTemplate
(
&
buf
,
c
.
TplName
,
c
.
Data
)
if
err
!=
nil
{
Trace
(
"template Execute err:"
,
err
)
return
nil
,
err
}
}
return
buf
.
Bytes
(),
nil
}
...
...
template.go
View file @
1f46c1d2
...
...
@@ -30,13 +30,21 @@ import (
var
(
beegoTplFuncMap
=
make
(
template
.
FuncMap
)
//
B
eeTemplates caching map and supported template file extensions.
B
eeTemplates
=
make
(
map
[
string
]
*
template
.
Template
)
templatesLock
sync
.
Mutex
//
B
eeTemplateExt stores the template extension which will build
B
eeTemplateExt
=
[]
string
{
"tpl"
,
"html"
}
//
b
eeTemplates caching map and supported template file extensions.
b
eeTemplates
=
make
(
map
[
string
]
*
template
.
Template
)
templatesLock
sync
.
RW
Mutex
//
b
eeTemplateExt stores the template extension which will build
b
eeTemplateExt
=
[]
string
{
"tpl"
,
"html"
}
)
func
getTemplateByName
(
name
string
)
*
template
.
Template
{
if
BConfig
.
RunMode
==
"dev"
{
templatesLock
.
RLock
()
defer
templatesLock
.
RUnlock
()
}
return
beeTemplates
[
name
]
}
func
init
()
{
beegoTplFuncMap
[
"dateformat"
]
=
DateFormat
beegoTplFuncMap
[
"date"
]
=
Date
...
...
@@ -55,7 +63,6 @@ func init() {
beegoTplFuncMap
[
"config"
]
=
GetConfig
beegoTplFuncMap
[
"map_get"
]
=
MapGet
// go1.2 added template funcs
// Comparisons
beegoTplFuncMap
[
"eq"
]
=
eq
// ==
beegoTplFuncMap
[
"ge"
]
=
ge
// >=
...
...
@@ -103,7 +110,7 @@ func (tf *templateFile) visit(paths string, f os.FileInfo, err error) error {
// HasTemplateExt return this path contains supported template extension of beego or not.
func
HasTemplateExt
(
paths
string
)
bool
{
for
_
,
v
:=
range
B
eeTemplateExt
{
for
_
,
v
:=
range
b
eeTemplateExt
{
if
strings
.
HasSuffix
(
paths
,
"."
+
v
)
{
return
true
}
...
...
@@ -113,12 +120,12 @@ func HasTemplateExt(paths string) bool {
// AddTemplateExt add new extension for template.
func
AddTemplateExt
(
ext
string
)
{
for
_
,
v
:=
range
B
eeTemplateExt
{
for
_
,
v
:=
range
b
eeTemplateExt
{
if
v
==
ext
{
return
}
}
BeeTemplateExt
=
append
(
B
eeTemplateExt
,
ext
)
beeTemplateExt
=
append
(
b
eeTemplateExt
,
ext
)
}
// BuildTemplate will build all template files in a directory.
...
...
@@ -149,7 +156,7 @@ func BuildTemplate(dir string, files ...string) error {
if
err
!=
nil
{
Trace
(
"parse template err:"
,
file
,
err
)
}
else
{
B
eeTemplates
[
file
]
=
t
b
eeTemplates
[
file
]
=
t
}
templatesLock
.
Unlock
()
}
...
...
template_test.go
View file @
1f46c1d2
...
...
@@ -70,10 +70,10 @@ func TestTemplate(t *testing.T) {
if
err
:=
BuildTemplate
(
dir
);
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
len
(
B
eeTemplates
)
!=
3
{
t
.
Fatalf
(
"should be 3 but got %v"
,
len
(
B
eeTemplates
))
if
len
(
b
eeTemplates
)
!=
3
{
t
.
Fatalf
(
"should be 3 but got %v"
,
len
(
b
eeTemplates
))
}
if
err
:=
B
eeTemplates
[
"index.tpl"
]
.
ExecuteTemplate
(
os
.
Stdout
,
"index.tpl"
,
nil
);
err
!=
nil
{
if
err
:=
b
eeTemplates
[
"index.tpl"
]
.
ExecuteTemplate
(
os
.
Stdout
,
"index.tpl"
,
nil
);
err
!=
nil
{
t
.
Fatal
(
err
)
}
for
_
,
name
:=
range
files
{
...
...
@@ -126,7 +126,7 @@ func TestRelativeTemplate(t *testing.T) {
if
err
:=
BuildTemplate
(
dir
,
files
[
1
]);
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
err
:=
B
eeTemplates
[
"easyui/rbac/user.tpl"
]
.
ExecuteTemplate
(
os
.
Stdout
,
"easyui/rbac/user.tpl"
,
nil
);
err
!=
nil
{
if
err
:=
b
eeTemplates
[
"easyui/rbac/user.tpl"
]
.
ExecuteTemplate
(
os
.
Stdout
,
"easyui/rbac/user.tpl"
,
nil
);
err
!=
nil
{
t
.
Fatal
(
err
)
}
for
_
,
name
:=
range
files
{
...
...
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