fix #260
Showing
2 changed files
with
69 additions
and
21 deletions
| ... | @@ -133,8 +133,16 @@ func BuildTemplate(dir string) error { | ... | @@ -133,8 +133,16 @@ func BuildTemplate(dir string) error { |
| 133 | return nil | 133 | return nil |
| 134 | } | 134 | } |
| 135 | 135 | ||
| 136 | func getTplDeep(root, file string, t *template.Template) (*template.Template, [][]string, error) { | 136 | func getTplDeep(root, file, parent string, t *template.Template) (*template.Template, [][]string, error) { |
| 137 | fileabspath := filepath.Join(root, file) | 137 | var fileabspath string |
| 138 | if filepath.HasPrefix(file, "../") { | ||
| 139 | fileabspath = filepath.Join(root, filepath.Dir(parent), file) | ||
| 140 | } else { | ||
| 141 | fileabspath = filepath.Join(root, file) | ||
| 142 | } | ||
| 143 | if e, _ := FileExists(fileabspath); !e { | ||
| 144 | panic("can't find template file" + file) | ||
| 145 | } | ||
| 138 | data, err := ioutil.ReadFile(fileabspath) | 146 | data, err := ioutil.ReadFile(fileabspath) |
| 139 | if err != nil { | 147 | if err != nil { |
| 140 | return nil, [][]string{}, err | 148 | return nil, [][]string{}, err |
| ... | @@ -154,23 +162,10 @@ func getTplDeep(root, file string, t *template.Template) (*template.Template, [] | ... | @@ -154,23 +162,10 @@ func getTplDeep(root, file string, t *template.Template) (*template.Template, [] |
| 154 | if !HasTemplateEXt(m[1]) { | 162 | if !HasTemplateEXt(m[1]) { |
| 155 | continue | 163 | continue |
| 156 | } | 164 | } |
| 157 | if e, _ := FileExists(filepath.Join(root, m[1])); e { | 165 | t, _, err = getTplDeep(root, m[1], file, t) |
| 158 | t, _, err = getTplDeep(root, m[1], t) | 166 | if err != nil { |
| 159 | if err != nil { | 167 | return nil, [][]string{}, err |
| 160 | return nil, [][]string{}, err | ||
| 161 | } | ||
| 162 | } else { | ||
| 163 | relativefile := filepath.Join(filepath.Dir(file), m[1]) | ||
| 164 | if e, _ := FileExists(relativefile); e { | ||
| 165 | t, _, err = getTplDeep(root, relativefile, t) | ||
| 166 | if err != nil { | ||
| 167 | return nil, [][]string{}, err | ||
| 168 | } | ||
| 169 | } else { | ||
| 170 | panic("can't find template file" + m[1]) | ||
| 171 | } | ||
| 172 | } | 168 | } |
| 173 | |||
| 174 | } | 169 | } |
| 175 | } | 170 | } |
| 176 | return t, allsub, nil | 171 | return t, allsub, nil |
| ... | @@ -179,7 +174,7 @@ func getTplDeep(root, file string, t *template.Template) (*template.Template, [] | ... | @@ -179,7 +174,7 @@ func getTplDeep(root, file string, t *template.Template) (*template.Template, [] |
| 179 | func getTemplate(root, file string, others ...string) (t *template.Template, err error) { | 174 | func getTemplate(root, file string, others ...string) (t *template.Template, err error) { |
| 180 | t = template.New(file).Delims(TemplateLeft, TemplateRight).Funcs(beegoTplFuncMap) | 175 | t = template.New(file).Delims(TemplateLeft, TemplateRight).Funcs(beegoTplFuncMap) |
| 181 | var submods [][]string | 176 | var submods [][]string |
| 182 | t, submods, err = getTplDeep(root, file, t) | 177 | t, submods, err = getTplDeep(root, file, "", t) |
| 183 | if err != nil { | 178 | if err != nil { |
| 184 | return nil, err | 179 | return nil, err |
| 185 | } | 180 | } |
| ... | @@ -203,7 +198,7 @@ func _getTemplate(t0 *template.Template, root string, submods [][]string, others | ... | @@ -203,7 +198,7 @@ func _getTemplate(t0 *template.Template, root string, submods [][]string, others |
| 203 | for _, otherfile := range others { | 198 | for _, otherfile := range others { |
| 204 | if otherfile == m[1] { | 199 | if otherfile == m[1] { |
| 205 | var submods1 [][]string | 200 | var submods1 [][]string |
| 206 | t, submods1, err = getTplDeep(root, otherfile, t) | 201 | t, submods1, err = getTplDeep(root, otherfile, "", t) |
| 207 | if err != nil { | 202 | if err != nil { |
| 208 | Trace("template parse file err:", err) | 203 | Trace("template parse file err:", err) |
| 209 | } else if submods1 != nil && len(submods1) > 0 { | 204 | } else if submods1 != nil && len(submods1) > 0 { |
| ... | @@ -224,7 +219,7 @@ func _getTemplate(t0 *template.Template, root string, submods [][]string, others | ... | @@ -224,7 +219,7 @@ func _getTemplate(t0 *template.Template, root string, submods [][]string, others |
| 224 | for _, sub := range allsub { | 219 | for _, sub := range allsub { |
| 225 | if len(sub) == 2 && sub[1] == m[1] { | 220 | if len(sub) == 2 && sub[1] == m[1] { |
| 226 | var submods1 [][]string | 221 | var submods1 [][]string |
| 227 | t, submods1, err = getTplDeep(root, otherfile, t) | 222 | t, submods1, err = getTplDeep(root, otherfile, "", t) |
| 228 | if err != nil { | 223 | if err != nil { |
| 229 | Trace("template parse file err:", err) | 224 | Trace("template parse file err:", err) |
| 230 | } else if submods1 != nil && len(submods1) > 0 { | 225 | } else if submods1 != nil && len(submods1) > 0 { | ... | ... |
| ... | @@ -67,3 +67,56 @@ func TestTemplate(t *testing.T) { | ... | @@ -67,3 +67,56 @@ func TestTemplate(t *testing.T) { |
| 67 | } | 67 | } |
| 68 | os.RemoveAll(dir) | 68 | os.RemoveAll(dir) |
| 69 | } | 69 | } |
| 70 | |||
| 71 | var menu string = `<div class="menu"> | ||
| 72 | <ul> | ||
| 73 | <li>menu1</li> | ||
| 74 | <li>menu2</li> | ||
| 75 | <li>menu3</li> | ||
| 76 | </ul> | ||
| 77 | </div> | ||
| 78 | ` | ||
| 79 | var user string = `<!DOCTYPE html> | ||
| 80 | <html> | ||
| 81 | <head> | ||
| 82 | <title>beego welcome template</title> | ||
| 83 | </head> | ||
| 84 | <body> | ||
| 85 | {{template "../public/menu.tpl"}} | ||
| 86 | </body> | ||
| 87 | </html> | ||
| 88 | ` | ||
| 89 | |||
| 90 | func TestRelativeTemplate(t *testing.T) { | ||
| 91 | dir := "_beeTmp" | ||
| 92 | files := []string{ | ||
| 93 | "easyui/public/menu.tpl", | ||
| 94 | "easyui/rbac/user.tpl", | ||
| 95 | } | ||
| 96 | if err := os.MkdirAll(dir, 0777); err != nil { | ||
| 97 | t.Fatal(err) | ||
| 98 | } | ||
| 99 | for k, name := range files { | ||
| 100 | os.MkdirAll(filepath.Dir(filepath.Join(dir, name)), 0777) | ||
| 101 | if f, err := os.Create(filepath.Join(dir, name)); err != nil { | ||
| 102 | t.Fatal(err) | ||
| 103 | } else { | ||
| 104 | if k == 0 { | ||
| 105 | f.WriteString(menu) | ||
| 106 | } else if k == 1 { | ||
| 107 | f.WriteString(user) | ||
| 108 | } | ||
| 109 | f.Close() | ||
| 110 | } | ||
| 111 | } | ||
| 112 | if err := BuildTemplate(dir); err != nil { | ||
| 113 | t.Fatal(err) | ||
| 114 | } | ||
| 115 | if err := BeeTemplates["easyui/rbac/user.tpl"].ExecuteTemplate(os.Stdout, "easyui/rbac/user.tpl", nil); err != nil { | ||
| 116 | t.Fatal(err) | ||
| 117 | } | ||
| 118 | for _, name := range files { | ||
| 119 | os.RemoveAll(filepath.Join(dir, name)) | ||
| 120 | } | ||
| 121 | os.RemoveAll(dir) | ||
| 122 | } | ... | ... |
-
Please register or sign in to post a comment