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
7aae58a5
Commit
7aae58a5
authored
Nov 05, 2018
by
Viktor Vassilyev
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(Template): create interface FileSystem for to create custom fs
parent
053a0753
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
69 additions
and
18 deletions
+69
-18
fs.go
fs.go
+30
-0
template.go
template.go
+38
-17
template_test.go
template_test.go
+1
-1
No files found.
fs.go
0 → 100644
View file @
7aae58a5
package
beego
import
(
"net/http"
"os"
"path/filepath"
)
type
IFileSystem
interface
{
http
.
FileSystem
Walk
(
string
,
filepath
.
WalkFunc
)
error
}
// A File is returned by a FileSystem's Open method and can be
// served by the FileServer implementation.
//
// The methods should behave the same as those on an *os.File.
type
File
struct
{
*
os
.
File
}
type
FileSystem
struct
{
}
func
(
d
FileSystem
)
Open
(
name
string
)
(
http
.
File
,
error
)
{
return
os
.
Open
(
name
)
}
func
(
d
FileSystem
)
Walk
(
root
string
,
walkFn
filepath
.
WalkFunc
)
error
{
return
filepath
.
Walk
(
root
,
walkFn
)
}
template.go
View file @
7aae58a5
...
...
@@ -40,6 +40,7 @@ var (
beeTemplateExt
=
[]
string
{
"tpl"
,
"html"
}
// beeTemplatePreprocessors stores associations of extension -> preprocessor handler
beeTemplateEngines
=
map
[
string
]
templatePreProcessor
{}
beeTemplateFS
=
defaultFSFunc
)
// ExecuteTemplate applies the template with name to the specified data object,
...
...
@@ -181,7 +182,15 @@ func lockViewPaths() {
// BuildTemplate will build all template files in a directory.
// it makes beego can render any template file in view directory.
func
BuildTemplate
(
dir
string
,
files
...
string
)
error
{
if
_
,
err
:=
os
.
Stat
(
dir
);
err
!=
nil
{
var
err
error
fs
:=
beeTemplateFS
()
f
,
err
:=
fs
.
Open
(
dir
)
if
err
!=
nil
{
return
errors
.
New
(
"dir open err"
)
}
defer
f
.
Close
()
if
_
,
err
:=
f
.
Stat
();
err
!=
nil
{
if
os
.
IsNotExist
(
err
)
{
return
nil
}
...
...
@@ -195,7 +204,7 @@ func BuildTemplate(dir string, files ...string) error {
root
:
dir
,
files
:
make
(
map
[
string
][]
string
),
}
err
:=
filepath
.
Walk
(
dir
,
func
(
path
string
,
f
os
.
FileInfo
,
err
error
)
error
{
err
=
fs
.
Walk
(
dir
,
func
(
path
string
,
f
os
.
FileInfo
,
err
error
)
error
{
return
self
.
visit
(
path
,
f
,
err
)
})
if
err
!=
nil
{
...
...
@@ -210,11 +219,11 @@ func BuildTemplate(dir string, files ...string) error {
ext
:=
filepath
.
Ext
(
file
)
var
t
*
template
.
Template
if
len
(
ext
)
==
0
{
t
,
err
=
getTemplate
(
self
.
root
,
file
,
v
...
)
t
,
err
=
getTemplate
(
self
.
root
,
f
s
,
f
ile
,
v
...
)
}
else
if
fn
,
ok
:=
beeTemplateEngines
[
ext
[
1
:
]];
ok
{
t
,
err
=
fn
(
self
.
root
,
file
,
beegoTplFuncMap
)
}
else
{
t
,
err
=
getTemplate
(
self
.
root
,
file
,
v
...
)
t
,
err
=
getTemplate
(
self
.
root
,
f
s
,
f
ile
,
v
...
)
}
if
err
!=
nil
{
logs
.
Error
(
"parse template err:"
,
file
,
err
)
...
...
@@ -229,9 +238,10 @@ func BuildTemplate(dir string, files ...string) error {
return
nil
}
func
getTplDeep
(
root
,
file
,
parent
string
,
t
*
template
.
Template
)
(
*
template
.
Template
,
[][]
string
,
error
)
{
func
getTplDeep
(
root
string
,
fs
IFileSystem
,
file
string
,
parent
string
,
t
*
template
.
Template
)
(
*
template
.
Template
,
[][]
string
,
error
)
{
var
fileAbsPath
string
var
rParent
string
var
err
error
if
filepath
.
HasPrefix
(
file
,
"../"
)
{
rParent
=
filepath
.
Join
(
filepath
.
Dir
(
parent
),
file
)
fileAbsPath
=
filepath
.
Join
(
root
,
filepath
.
Dir
(
parent
),
file
)
...
...
@@ -239,10 +249,11 @@ func getTplDeep(root, file, parent string, t *template.Template) (*template.Temp
rParent
=
file
fileAbsPath
=
filepath
.
Join
(
root
,
file
)
}
if
e
:=
utils
.
FileExists
(
fileAbsPath
);
!
e
{
f
,
err
:=
fs
.
Open
(
fileAbsPath
)
if
err
!=
nil
{
panic
(
"can't find template file:"
+
file
)
}
data
,
err
:=
ioutil
.
Read
File
(
fileAbsPath
)
data
,
err
:=
ioutil
.
Read
All
(
f
)
if
err
!=
nil
{
return
nil
,
[][]
string
{},
err
}
...
...
@@ -261,7 +272,7 @@ func getTplDeep(root, file, parent string, t *template.Template) (*template.Temp
if
!
HasTemplateExt
(
m
[
1
])
{
continue
}
_
,
_
,
err
=
getTplDeep
(
root
,
m
[
1
],
rParent
,
t
)
_
,
_
,
err
=
getTplDeep
(
root
,
fs
,
m
[
1
],
rParent
,
t
)
if
err
!=
nil
{
return
nil
,
[][]
string
{},
err
}
...
...
@@ -270,14 +281,14 @@ func getTplDeep(root, file, parent string, t *template.Template) (*template.Temp
return
t
,
allSub
,
nil
}
func
getTemplate
(
root
,
file
string
,
others
...
string
)
(
t
*
template
.
Template
,
err
error
)
{
func
getTemplate
(
root
string
,
fs
IFileSystem
,
file
string
,
others
...
string
)
(
t
*
template
.
Template
,
err
error
)
{
t
=
template
.
New
(
file
)
.
Delims
(
BConfig
.
WebConfig
.
TemplateLeft
,
BConfig
.
WebConfig
.
TemplateRight
)
.
Funcs
(
beegoTplFuncMap
)
var
subMods
[][]
string
t
,
subMods
,
err
=
getTplDeep
(
root
,
file
,
""
,
t
)
t
,
subMods
,
err
=
getTplDeep
(
root
,
f
s
,
f
ile
,
""
,
t
)
if
err
!=
nil
{
return
nil
,
err
}
t
,
err
=
_getTemplate
(
t
,
root
,
subMods
,
others
...
)
t
,
err
=
_getTemplate
(
t
,
root
,
fs
,
subMods
,
others
...
)
if
err
!=
nil
{
return
nil
,
err
...
...
@@ -285,7 +296,7 @@ func getTemplate(root, file string, others ...string) (t *template.Template, err
return
}
func
_getTemplate
(
t0
*
template
.
Template
,
root
string
,
subMods
[][]
string
,
others
...
string
)
(
t
*
template
.
Template
,
err
error
)
{
func
_getTemplate
(
t0
*
template
.
Template
,
root
string
,
fs
IFileSystem
,
subMods
[][]
string
,
others
...
string
)
(
t
*
template
.
Template
,
err
error
)
{
t
=
t0
for
_
,
m
:=
range
subMods
{
if
len
(
m
)
==
2
{
...
...
@@ -297,11 +308,11 @@ func _getTemplate(t0 *template.Template, root string, subMods [][]string, others
for
_
,
otherFile
:=
range
others
{
if
otherFile
==
m
[
1
]
{
var
subMods1
[][]
string
t
,
subMods1
,
err
=
getTplDeep
(
root
,
otherFile
,
""
,
t
)
t
,
subMods1
,
err
=
getTplDeep
(
root
,
fs
,
otherFile
,
""
,
t
)
if
err
!=
nil
{
logs
.
Trace
(
"template parse file err:"
,
err
)
}
else
if
len
(
subMods1
)
>
0
{
t
,
err
=
_getTemplate
(
t
,
root
,
subMods1
,
others
...
)
t
,
err
=
_getTemplate
(
t
,
root
,
fs
,
subMods1
,
others
...
)
}
break
}
...
...
@@ -310,20 +321,21 @@ func _getTemplate(t0 *template.Template, root string, subMods [][]string, others
for
_
,
otherFile
:=
range
others
{
var
data
[]
byte
fileAbsPath
:=
filepath
.
Join
(
root
,
otherFile
)
data
,
err
=
ioutil
.
ReadFile
(
fileAbsPath
)
f
,
err
:=
fs
.
Open
(
fileAbsPath
)
if
err
!=
nil
{
continue
}
data
,
err
=
ioutil
.
ReadAll
(
f
)
reg
:=
regexp
.
MustCompile
(
BConfig
.
WebConfig
.
TemplateLeft
+
"[ ]*define[ ]+
\"
([^
\"
]+)
\"
"
)
allSub
:=
reg
.
FindAllStringSubmatch
(
string
(
data
),
-
1
)
for
_
,
sub
:=
range
allSub
{
if
len
(
sub
)
==
2
&&
sub
[
1
]
==
m
[
1
]
{
var
subMods1
[][]
string
t
,
subMods1
,
err
=
getTplDeep
(
root
,
otherFile
,
""
,
t
)
t
,
subMods1
,
err
=
getTplDeep
(
root
,
fs
,
otherFile
,
""
,
t
)
if
err
!=
nil
{
logs
.
Trace
(
"template parse file err:"
,
err
)
}
else
if
len
(
subMods1
)
>
0
{
t
,
err
=
_getTemplate
(
t
,
root
,
subMods1
,
others
...
)
t
,
err
=
_getTemplate
(
t
,
root
,
fs
,
subMods1
,
others
...
)
}
break
}
...
...
@@ -335,6 +347,15 @@ func _getTemplate(t0 *template.Template, root string, subMods [][]string, others
return
}
type
templateFSFunc
func
()
IFileSystem
func
defaultFSFunc
()
IFileSystem
{
return
FileSystem
{}
}
func
SetTemplateFSFunc
(
fnt
templateFSFunc
)
{
beeTemplateFS
=
fnt
}
// SetViewsPath sets view directory path in beego application.
func
SetViewsPath
(
path
string
)
*
App
{
BConfig
.
WebConfig
.
ViewsPath
=
path
...
...
template_test.go
View file @
7aae58a5
...
...
@@ -105,7 +105,7 @@ var user = `<!DOCTYPE html>
func
TestRelativeTemplate
(
t
*
testing
.
T
)
{
dir
:=
"_beeTmp"
os
.
Mkdir
(
dir
,
0777
)
//Just add dir to known viewPaths
if
err
:=
AddViewPath
(
dir
);
err
!=
nil
{
t
.
Fatal
(
err
)
...
...
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