Skip to content

Commit fdbd646

Browse files
Group template helper functions, remove Printf, improve template error messages (#23982)
Follow #23328 Major changes: * Group the function in `templates/help.go` by their purposes. It could make future work easier. * Remove the `Printf` helper function, there is already a builtin `printf`. * Remove `DiffStatsWidth`, replace with `Eval` in template * Rename the `NewTextFuncMap` to `mailSubjectTextFuncMap`, it's for subject text template only, no need to make it support HTML functions. ---- And fine tune template error messages, to make it more friendly to developers and users. ![image](https://user-images.githubusercontent.com/2114189/230714245-4fd202d1-2b25-41b2-8be5-03c5fee45091.png) ![image](https://user-images.githubusercontent.com/2114189/230714277-66783577-2a03-49d5-8e8c-ceba5e07a2d4.png) --------- Co-authored-by: silverwind <[email protected]>
1 parent cf5a281 commit fdbd646

File tree

15 files changed

+297
-285
lines changed

15 files changed

+297
-285
lines changed

modules/context/context.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ import (
4747

4848
// Render represents a template render
4949
type Render interface {
50-
TemplateLookup(tmpl string) *template.Template
50+
TemplateLookup(tmpl string) (*template.Template, error)
5151
HTML(w io.Writer, status int, name string, data interface{}) error
5252
}
5353

@@ -228,7 +228,7 @@ func (ctx *Context) HTML(status int, name base.TplName) {
228228
}
229229
if err := ctx.Render.HTML(ctx.Resp, status, string(name), templates.BaseVars().Merge(ctx.Data)); err != nil {
230230
if status == http.StatusInternalServerError && name == base.TplName("status/500") {
231-
ctx.PlainText(http.StatusInternalServerError, "Unable to find status/500 template")
231+
ctx.PlainText(http.StatusInternalServerError, "Unable to find HTML templates, the template system is not initialized, or Gitea can't find your template files.")
232232
return
233233
}
234234
if execErr, ok := err.(texttemplate.ExecError); ok {
@@ -247,7 +247,7 @@ func (ctx *Context) HTML(status int, name base.TplName) {
247247
if errorTemplateName != string(name) {
248248
filename += " (subtemplate of " + string(name) + ")"
249249
}
250-
err = fmt.Errorf("%w\nin template file %s:\n%s", err, filename, templates.GetLineFromTemplate(errorTemplateName, line, target, pos))
250+
err = fmt.Errorf("failed to render %s, error: %w:\n%s", filename, err, templates.GetLineFromTemplate(errorTemplateName, line, target, pos))
251251
} else {
252252
filename, filenameErr := templates.GetAssetFilename("templates/" + execErr.Name + ".tmpl")
253253
if filenameErr != nil {
@@ -256,7 +256,7 @@ func (ctx *Context) HTML(status int, name base.TplName) {
256256
if execErr.Name != string(name) {
257257
filename += " (subtemplate of " + string(name) + ")"
258258
}
259-
err = fmt.Errorf("%w\nin template file %s", err, filename)
259+
err = fmt.Errorf("failed to render %s, error: %w", filename, err)
260260
}
261261
}
262262
ctx.ServerError("Render failed", err)

modules/templates/dynamic.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,13 @@
66
package templates
77

88
import (
9-
"html/template"
109
"io/fs"
1110
"os"
1211
"path/filepath"
13-
texttmpl "text/template"
1412

1513
"code.gitea.io/gitea/modules/setting"
1614
)
1715

18-
var (
19-
subjectTemplates = texttmpl.New("")
20-
bodyTemplates = template.New("")
21-
)
22-
2316
// GetAsset returns asset content via name
2417
func GetAsset(name string) ([]byte, error) {
2518
bs, err := os.ReadFile(filepath.Join(setting.CustomPath, name))

0 commit comments

Comments
 (0)