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
ccc84dd8
Commit
ccc84dd8
authored
Mar 30, 2016
by
astaxie
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1762 from saturn4er/htmlEngines
Implemented possibility to add custom template engines
parents
734bbb33
adaa4ab9
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
24 additions
and
3 deletions
+24
-3
template.go
template.go
+24
-3
No files found.
template.go
View file @
ccc84dd8
...
@@ -33,10 +33,12 @@ import (
...
@@ -33,10 +33,12 @@ import (
var
(
var
(
beegoTplFuncMap
=
make
(
template
.
FuncMap
)
beegoTplFuncMap
=
make
(
template
.
FuncMap
)
// beeTemplates caching map and supported template file extensions.
// beeTemplates caching map and supported template file extensions.
beeTemplates
=
make
(
map
[
string
]
*
template
.
Template
)
beeTemplates
=
make
(
map
[
string
]
TemplateRenderer
)
templatesLock
sync
.
RWMutex
templatesLock
sync
.
RWMutex
// beeTemplateExt stores the template extension which will build
// beeTemplateExt stores the template extension which will build
beeTemplateExt
=
[]
string
{
"tpl"
,
"html"
}
beeTemplateExt
=
[]
string
{
"tpl"
,
"html"
}
// beeTemplatePreprocessors stores associations of extension -> preprocessor handler
beeTemplateEngines
=
map
[
string
]
templateHandler
{}
)
)
func
executeTemplate
(
wr
io
.
Writer
,
name
string
,
data
interface
{})
error
{
func
executeTemplate
(
wr
io
.
Writer
,
name
string
,
data
interface
{})
error
{
...
@@ -89,6 +91,10 @@ func AddFuncMap(key string, fn interface{}) error {
...
@@ -89,6 +91,10 @@ func AddFuncMap(key string, fn interface{}) error {
return
nil
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
templateFile
struct
{
type
templateFile
struct
{
root
string
root
string
files
map
[
string
][]
string
files
map
[
string
][]
string
...
@@ -157,11 +163,20 @@ func BuildTemplate(dir string, files ...string) error {
...
@@ -157,11 +163,20 @@ func BuildTemplate(dir string, files ...string) error {
fmt
.
Printf
(
"filepath.Walk() returned %v
\n
"
,
err
)
fmt
.
Printf
(
"filepath.Walk() returned %v
\n
"
,
err
)
return
err
return
err
}
}
buildAllFiles
:=
len
(
files
)
==
0
for
_
,
v
:=
range
self
.
files
{
for
_
,
v
:=
range
self
.
files
{
for
_
,
file
:=
range
v
{
for
_
,
file
:=
range
v
{
if
len
(
files
)
==
0
||
utils
.
InSlice
(
file
,
files
)
{
if
buildAllFiles
||
utils
.
InSlice
(
file
,
files
)
{
templatesLock
.
Lock
()
templatesLock
.
Lock
()
t
,
err
:=
getTemplate
(
self
.
root
,
file
,
v
...
)
ext
:=
filepath
.
Ext
(
file
)
var
t
TemplateRenderer
if
len
(
ext
)
==
0
{
t
,
err
=
getTemplate
(
self
.
root
,
file
,
v
...
)
}
else
if
fn
,
ok
:=
beeTemplateEngines
[
ext
[
1
:
]];
ok
{
t
,
err
=
fn
(
self
.
root
,
file
,
beegoTplFuncMap
)
}
else
{
t
,
err
=
getTemplate
(
self
.
root
,
file
,
v
...
)
}
if
err
!=
nil
{
if
err
!=
nil
{
logs
.
Trace
(
"parse template err:"
,
file
,
err
)
logs
.
Trace
(
"parse template err:"
,
file
,
err
)
}
else
{
}
else
{
...
@@ -306,3 +321,9 @@ func DelStaticPath(url string) *App {
...
@@ -306,3 +321,9 @@ func DelStaticPath(url string) *App {
delete
(
BConfig
.
WebConfig
.
StaticDir
,
url
)
delete
(
BConfig
.
WebConfig
.
StaticDir
,
url
)
return
BeeApp
return
BeeApp
}
}
func
AddTemplateEngine
(
extension
string
,
fn
templateHandler
)
*
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