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
7b051e7a
Commit
7b051e7a
authored
May 24, 2016
by
astaxie
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1940 from MachineShop-IOT/develop
More flexible support for template engines
parents
cef91db2
24b88706
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
12 deletions
+21
-12
template.go
template.go
+21
-12
No files found.
template.go
View file @
7b051e7a
...
...
@@ -33,12 +33,12 @@ import (
var
(
beegoTplFuncMap
=
make
(
template
.
FuncMap
)
// beeTemplates caching map and supported template file extensions.
beeTemplates
=
make
(
map
[
string
]
TemplateRenderer
)
beeTemplates
=
make
(
map
[
string
]
*
template
.
Template
)
templatesLock
sync
.
RWMutex
// beeTemplateExt stores the template extension which will build
beeTemplateExt
=
[]
string
{
"tpl"
,
"html"
}
// beeTemplatePreprocessors stores associations of extension -> preprocessor handler
beeTemplateEngines
=
map
[
string
]
template
Handle
r
{}
beeTemplateEngines
=
map
[
string
]
template
PreProcesso
r
{}
)
// ExecuteTemplate applies the template with name to the specified data object,
...
...
@@ -50,11 +50,22 @@ func ExecuteTemplate(wr io.Writer, name string, data interface{}) error {
defer
templatesLock
.
RUnlock
()
}
if
t
,
ok
:=
beeTemplates
[
name
];
ok
{
err
:=
t
.
ExecuteTemplate
(
wr
,
name
,
data
)
if
err
!=
nil
{
logs
.
Trace
(
"template Execute err:"
,
err
)
if
t
.
Lookup
(
name
)
!=
nil
{
err
:=
t
.
ExecuteTemplate
(
wr
,
name
,
data
)
if
err
!=
nil
{
logs
.
Trace
(
"template Execute err:"
,
err
)
}
return
err
}
else
{
err
:=
t
.
Execute
(
wr
,
data
)
if
err
!=
nil
{
if
err
!=
nil
{
logs
.
Trace
(
"template Execute err:"
,
err
)
}
return
err
}
}
return
err
return
nil
}
panic
(
"can't find templatefile in the path:"
+
name
)
}
...
...
@@ -94,10 +105,8 @@ func AddFuncMap(key string, fn interface{}) error {
return
nil
}
type
templateHandler
func
(
root
,
path
string
,
funcs
template
.
FuncMap
)
(
TemplateRenderer
,
error
)
type
TemplateRenderer
interface
{
ExecuteTemplate
(
wr
io
.
Writer
,
name
string
,
data
interface
{})
error
}
type
templatePreProcessor
func
(
root
,
path
string
,
funcs
template
.
FuncMap
)
(
*
template
.
Template
,
error
)
type
templateFile
struct
{
root
string
files
map
[
string
][]
string
...
...
@@ -172,7 +181,7 @@ func BuildTemplate(dir string, files ...string) error {
if
buildAllFiles
||
utils
.
InSlice
(
file
,
files
)
{
templatesLock
.
Lock
()
ext
:=
filepath
.
Ext
(
file
)
var
t
TemplateRenderer
var
t
*
template
.
Template
if
len
(
ext
)
==
0
{
t
,
err
=
getTemplate
(
self
.
root
,
file
,
v
...
)
}
else
if
fn
,
ok
:=
beeTemplateEngines
[
ext
[
1
:
]];
ok
{
...
...
@@ -325,7 +334,7 @@ func DelStaticPath(url string) *App {
return
BeeApp
}
func
AddTemplateEngine
(
extension
string
,
fn
template
Handle
r
)
*
App
{
func
AddTemplateEngine
(
extension
string
,
fn
template
PreProcesso
r
)
*
App
{
AddTemplateExt
(
extension
)
beeTemplateEngines
[
extension
]
=
fn
return
BeeApp
...
...
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