summaryrefslogtreecommitdiffstats
path: root/modules/templates/helper.go
diff options
context:
space:
mode:
Diffstat (limited to 'modules/templates/helper.go')
-rw-r--r--modules/templates/helper.go57
1 files changed, 18 insertions, 39 deletions
diff --git a/modules/templates/helper.go b/modules/templates/helper.go
index 1686e54834..56be050481 100644
--- a/modules/templates/helper.go
+++ b/modules/templates/helper.go
@@ -42,6 +42,7 @@ import (
"code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/svg"
+ "code.gitea.io/gitea/modules/templates/eval"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/services/gitdiff"
@@ -105,24 +106,9 @@ func NewFuncMap() []template.FuncMap {
"TimeSinceUnix": timeutil.TimeSinceUnix,
"FileSize": base.FileSize,
"LocaleNumber": LocaleNumber,
- "Subtract": base.Subtract,
"EntryIcon": base.EntryIcon,
"MigrationIcon": MigrationIcon,
- "Add": func(a ...int) int {
- sum := 0
- for _, val := range a {
- sum += val
- }
- return sum
- },
- "Mul": func(a ...int) int {
- sum := 1
- for _, val := range a {
- sum *= val
- }
- return sum
- },
- "ActionIcon": ActionIcon,
+ "ActionIcon": ActionIcon,
"DateFmtLong": func(t time.Time) string {
return t.Format(time.RFC1123Z)
},
@@ -377,7 +363,7 @@ func NewFuncMap() []template.FuncMap {
"QueryEscape": url.QueryEscape,
"DotEscape": DotEscape,
"Iterate": func(arg interface{}) (items []int64) {
- count := util.ToInt64(arg)
+ count, _ := util.ToInt64(arg)
for i := int64(0); i < count; i++ {
items = append(items, i)
}
@@ -397,6 +383,7 @@ func NewFuncMap() []template.FuncMap {
curBranch,
)
},
+ "Eval": Eval,
}}
}
@@ -472,28 +459,8 @@ func NewTextFuncMap() []texttmpl.FuncMap {
}
return dict, nil
},
- "percentage": func(n int, values ...int) float32 {
- sum := 0
- for i := 0; i < len(values); i++ {
- sum += values[i]
- }
- return float32(n) * 100 / float32(sum)
- },
- "Add": func(a ...int) int {
- sum := 0
- for _, val := range a {
- sum += val
- }
- return sum
- },
- "Mul": func(a ...int) int {
- sum := 1
- for _, val := range a {
- sum *= val
- }
- return sum
- },
"QueryEscape": url.QueryEscape,
+ "Eval": Eval,
}}
}
@@ -944,6 +911,18 @@ func mirrorRemoteAddress(ctx context.Context, m *repo_model.Repository, remoteNa
// LocaleNumber renders a number with a Custom Element, browser will render it with a locale number
func LocaleNumber(v interface{}) template.HTML {
- num := util.ToInt64(v)
+ num, _ := util.ToInt64(v)
return template.HTML(fmt.Sprintf(`<gitea-locale-number data-number="%d">%d</gitea-locale-number>`, num, num))
}
+
+// Eval the expression and return the result, see the comment of eval.Expr for details.
+// To use this helper function in templates, pass each token as a separate parameter.
+//
+// {{ $int64 := Eval $var "+" 1 }}
+// {{ $float64 := Eval $var "+" 1.0 }}
+//
+// Golang's template supports comparable int types, so the int64 result can be used in later statements like {{if lt $int64 10}}
+func Eval(tokens ...any) (any, error) {
+ n, err := eval.Expr(tokens...)
+ return n.Value, err
+}