mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 07:00:55 +00:00
fix #260
This commit is contained in:
parent
4bcbc588fc
commit
c6167ef184
33
template.go
33
template.go
@ -133,8 +133,16 @@ func BuildTemplate(dir string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func getTplDeep(root, file string, t *template.Template) (*template.Template, [][]string, error) {
|
||||
fileabspath := filepath.Join(root, file)
|
||||
func getTplDeep(root, file, parent string, t *template.Template) (*template.Template, [][]string, error) {
|
||||
var fileabspath string
|
||||
if filepath.HasPrefix(file, "../") {
|
||||
fileabspath = filepath.Join(root, filepath.Dir(parent), file)
|
||||
} else {
|
||||
fileabspath = filepath.Join(root, file)
|
||||
}
|
||||
if e, _ := FileExists(fileabspath); !e {
|
||||
panic("can't find template file" + file)
|
||||
}
|
||||
data, err := ioutil.ReadFile(fileabspath)
|
||||
if err != nil {
|
||||
return nil, [][]string{}, err
|
||||
@ -154,23 +162,10 @@ func getTplDeep(root, file string, t *template.Template) (*template.Template, []
|
||||
if !HasTemplateEXt(m[1]) {
|
||||
continue
|
||||
}
|
||||
if e, _ := FileExists(filepath.Join(root, m[1])); e {
|
||||
t, _, err = getTplDeep(root, m[1], t)
|
||||
t, _, err = getTplDeep(root, m[1], file, t)
|
||||
if err != nil {
|
||||
return nil, [][]string{}, err
|
||||
}
|
||||
} else {
|
||||
relativefile := filepath.Join(filepath.Dir(file), m[1])
|
||||
if e, _ := FileExists(relativefile); e {
|
||||
t, _, err = getTplDeep(root, relativefile, t)
|
||||
if err != nil {
|
||||
return nil, [][]string{}, err
|
||||
}
|
||||
} else {
|
||||
panic("can't find template file" + m[1])
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return t, allsub, nil
|
||||
@ -179,7 +174,7 @@ func getTplDeep(root, file string, t *template.Template) (*template.Template, []
|
||||
func getTemplate(root, file string, others ...string) (t *template.Template, err error) {
|
||||
t = template.New(file).Delims(TemplateLeft, TemplateRight).Funcs(beegoTplFuncMap)
|
||||
var submods [][]string
|
||||
t, submods, err = getTplDeep(root, file, t)
|
||||
t, submods, err = getTplDeep(root, file, "", t)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -203,7 +198,7 @@ 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, otherfile, "", t)
|
||||
if err != nil {
|
||||
Trace("template parse file err:", err)
|
||||
} else if submods1 != nil && len(submods1) > 0 {
|
||||
@ -224,7 +219,7 @@ func _getTemplate(t0 *template.Template, root string, submods [][]string, others
|
||||
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, otherfile, "", t)
|
||||
if err != nil {
|
||||
Trace("template parse file err:", err)
|
||||
} else if submods1 != nil && len(submods1) > 0 {
|
||||
|
@ -67,3 +67,56 @@ func TestTemplate(t *testing.T) {
|
||||
}
|
||||
os.RemoveAll(dir)
|
||||
}
|
||||
|
||||
var menu string = `<div class="menu">
|
||||
<ul>
|
||||
<li>menu1</li>
|
||||
<li>menu2</li>
|
||||
<li>menu3</li>
|
||||
</ul>
|
||||
</div>
|
||||
`
|
||||
var user string = `<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>beego welcome template</title>
|
||||
</head>
|
||||
<body>
|
||||
{{template "../public/menu.tpl"}}
|
||||
</body>
|
||||
</html>
|
||||
`
|
||||
|
||||
func TestRelativeTemplate(t *testing.T) {
|
||||
dir := "_beeTmp"
|
||||
files := []string{
|
||||
"easyui/public/menu.tpl",
|
||||
"easyui/rbac/user.tpl",
|
||||
}
|
||||
if err := os.MkdirAll(dir, 0777); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for k, name := range files {
|
||||
os.MkdirAll(filepath.Dir(filepath.Join(dir, name)), 0777)
|
||||
if f, err := os.Create(filepath.Join(dir, name)); err != nil {
|
||||
t.Fatal(err)
|
||||
} else {
|
||||
if k == 0 {
|
||||
f.WriteString(menu)
|
||||
} else if k == 1 {
|
||||
f.WriteString(user)
|
||||
}
|
||||
f.Close()
|
||||
}
|
||||
}
|
||||
if err := BuildTemplate(dir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := BeeTemplates["easyui/rbac/user.tpl"].ExecuteTemplate(os.Stdout, "easyui/rbac/user.tpl", nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, name := range files {
|
||||
os.RemoveAll(filepath.Join(dir, name))
|
||||
}
|
||||
os.RemoveAll(dir)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user