Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
二次元的罪恶王冠
beego
提交
f5adec31
B
beego
项目概览
二次元的罪恶王冠
/
beego
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
B
beego
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
f5adec31
编写于
3月 04, 2016
作者:
J
JessonChan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
improve the template reader function
上级
6747c55a
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
30 addition
and
52 deletion
+30
-52
controller.go
controller.go
+19
-49
template.go
template.go
+11
-3
未找到文件。
controller.go
浏览文件 @
f5adec31
...
...
@@ -197,35 +197,9 @@ func (c *Controller) RenderString() (string, error) {
// RenderBytes returns the bytes of rendered template string. Do not send out response.
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
}
if
BConfig
.
RunMode
==
DEV
{
buildFiles
:=
[]
string
{
c
.
TplName
}
if
c
.
LayoutSections
!=
nil
{
for
_
,
sectionTpl
:=
range
c
.
LayoutSections
{
if
sectionTpl
==
""
{
continue
}
buildFiles
=
append
(
buildFiles
,
sectionTpl
)
}
}
BuildTemplate
(
BConfig
.
WebConfig
.
ViewsPath
,
buildFiles
...
)
}
if
t
:=
getTemplateByName
(
c
.
TplName
);
t
==
nil
{
panic
(
"can't find templatefile in the path:"
+
c
.
TplName
)
}
else
{
err
=
t
.
ExecuteTemplate
(
&
buf
,
c
.
TplName
,
c
.
Data
)
if
err
!=
nil
{
Trace
(
"template Execute err:"
,
err
)
return
nil
,
err
}
}
buf
,
err
:=
c
.
renderTemplate
()
//if the controller has set layout, then first get the tplName's content set the content to the layout
if
err
==
nil
&&
c
.
Layout
!=
""
{
c
.
Data
[
"LayoutContent"
]
=
template
.
HTML
(
buf
.
String
())
if
c
.
LayoutSections
!=
nil
{
...
...
@@ -234,11 +208,9 @@ func (c *Controller) RenderBytes() ([]byte, error) {
c
.
Data
[
sectionName
]
=
""
continue
}
buf
.
Reset
()
err
=
getTemplateByName
(
sectionTpl
)
.
E
xecuteTemplate
(
&
buf
,
sectionTpl
,
c
.
Data
)
err
=
e
xecuteTemplate
(
&
buf
,
sectionTpl
,
c
.
Data
)
if
err
!=
nil
{
Trace
(
"template Execute err:"
,
err
)
return
nil
,
err
}
c
.
Data
[
sectionName
]
=
template
.
HTML
(
buf
.
String
())
...
...
@@ -246,31 +218,29 @@ func (c *Controller) RenderBytes() ([]byte, error) {
}
buf
.
Reset
()
err
=
getTemplateByName
(
c
.
Layout
)
.
ExecuteTemplate
(
&
buf
,
c
.
Layout
,
c
.
Data
)
if
err
!=
nil
{
Trace
(
"template Execute err:"
,
err
)
return
nil
,
err
}
return
buf
.
Bytes
(),
nil
executeTemplate
(
&
buf
,
c
.
Layout
,
c
.
Data
)
}
return
buf
.
Bytes
(),
err
}
func
(
c
*
Controller
)
renderTemplate
()
(
bytes
.
Buffer
,
error
)
{
var
buf
bytes
.
Buffer
if
c
.
TplName
==
""
{
c
.
TplName
=
strings
.
ToLower
(
c
.
controllerName
)
+
"/"
+
strings
.
ToLower
(
c
.
actionName
)
+
"."
+
c
.
TplExt
}
if
BConfig
.
RunMode
==
DEV
{
BuildTemplate
(
BConfig
.
WebConfig
.
ViewsPath
,
c
.
TplName
)
}
if
t
:=
getTemplateByName
(
c
.
TplName
);
t
==
nil
{
panic
(
"can't find templatefile in the path:"
+
c
.
TplName
)
}
else
{
buf
.
Reset
()
err
=
t
.
ExecuteTemplate
(
&
buf
,
c
.
TplName
,
c
.
Data
)
if
err
!=
nil
{
Trace
(
"template Execute err:"
,
err
)
return
nil
,
err
buildFiles
:=
[]
string
{
c
.
TplName
}
if
c
.
Layout
!=
""
&&
c
.
LayoutSections
!=
nil
{
for
_
,
sectionTpl
:=
range
c
.
LayoutSections
{
if
sectionTpl
==
""
{
continue
}
buildFiles
=
append
(
buildFiles
,
sectionTpl
)
}
}
BuildTemplate
(
BConfig
.
WebConfig
.
ViewsPath
,
buildFiles
...
)
}
return
buf
.
Bytes
(),
nil
return
buf
,
executeTemplate
(
&
buf
,
c
.
TplName
,
c
.
Data
)
}
// Redirect sends the redirection response to url with status code.
...
...
template.go
浏览文件 @
f5adec31
...
...
@@ -18,6 +18,7 @@ import (
"errors"
"fmt"
"html/template"
"io"
"io/ioutil"
"os"
"path/filepath"
...
...
@@ -37,12 +38,19 @@ var (
beeTemplateExt
=
[]
string
{
"tpl"
,
"html"
}
)
func
getTemplateByName
(
name
string
)
*
template
.
Template
{
if
BConfig
.
RunMode
==
"dev"
{
func
executeTemplate
(
wr
io
.
Writer
,
name
string
,
data
interface
{})
error
{
if
BConfig
.
RunMode
==
DEV
{
templatesLock
.
RLock
()
defer
templatesLock
.
RUnlock
()
}
return
beeTemplates
[
name
]
if
t
,
ok
:=
beeTemplates
[
name
];
ok
{
err
:=
t
.
ExecuteTemplate
(
wr
,
name
,
data
)
if
err
!=
nil
{
Trace
(
"template Execute err:"
,
err
)
}
return
err
}
panic
(
"can't find templatefile in the path:"
+
name
)
}
func
init
()
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录