diff --git a/modules/templates/helper.go b/modules/templates/helper.go index 94464fe628..8779de69ca 100644 --- a/modules/templates/helper.go +++ b/modules/templates/helper.go @@ -9,6 +9,7 @@ import ( "html" "html/template" "net/url" + "reflect" "slices" "strings" "time" @@ -237,8 +238,8 @@ func DotEscape(raw string) string { // Iif is an "inline-if", similar util.Iif[T] but templates need the non-generic version, // and it could be simply used as "{{Iif expr trueVal}}" (omit the falseVal). -func Iif(condition bool, vals ...any) any { - if condition { +func Iif(condition any, vals ...any) any { + if IsTruthy(condition) { return vals[0] } else if len(vals) > 1 { return vals[1] @@ -246,6 +247,32 @@ func Iif(condition bool, vals ...any) any { return nil } +func IsTruthy(v any) bool { + if v == nil { + return false + } + + rv := reflect.ValueOf(v) + switch rv.Kind() { + case reflect.Bool: + return rv.Bool() + case reflect.String: + return rv.String() != "" + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + return rv.Int() != 0 + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + return rv.Uint() != 0 + case reflect.Float32, reflect.Float64: + return rv.Float() != 0 + case reflect.Slice, reflect.Array, reflect.Map: + return rv.Len() > 0 + case reflect.Ptr: + return !rv.IsNil() && IsTruthy(reflect.Indirect(rv).Interface()) + default: + return rv.Kind() == reflect.Struct && !rv.IsNil() + } +} + // 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. // diff --git a/modules/templates/helper_test.go b/modules/templates/helper_test.go index 0cefb7a6b2..c6c70cc18e 100644 --- a/modules/templates/helper_test.go +++ b/modules/templates/helper_test.go @@ -65,3 +65,18 @@ func TestHTMLFormat(t *testing.T) { func TestSanitizeHTML(t *testing.T) { assert.Equal(t, template.HTML(`link xss
inline
`), SanitizeHTML(`link xss
inline
`)) } + +func TestIsTruthy(t *testing.T) { + var test any + assert.Equal(t, false, IsTruthy(test)) + assert.Equal(t, false, IsTruthy(nil)) + assert.Equal(t, false, IsTruthy("")) + assert.Equal(t, true, IsTruthy("non-empty")) + assert.Equal(t, true, IsTruthy(-1)) + assert.Equal(t, false, IsTruthy(0)) + assert.Equal(t, true, IsTruthy(42)) + assert.Equal(t, false, IsTruthy(0.0)) + assert.Equal(t, true, IsTruthy(3.14)) + assert.Equal(t, false, IsTruthy([]int{})) + assert.Equal(t, true, IsTruthy([]int{1})) +} diff --git a/templates/admin/auth/list.tmpl b/templates/admin/auth/list.tmpl index 6483ec800c..174dda1e2a 100644 --- a/templates/admin/auth/list.tmpl +++ b/templates/admin/auth/list.tmpl @@ -25,7 +25,7 @@ {{.ID}} {{.Name}} {{.TypeName}} - {{if .IsActive}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}} + {{svg (Iif .IsActive "octicon-check" "octicon-x")}} {{DateTime "short" .UpdatedUnix}} {{DateTime "short" .CreatedUnix}} {{svg "octicon-pencil"}} diff --git a/templates/admin/config.tmpl b/templates/admin/config.tmpl index 8c16429920..197a6c6add 100644 --- a/templates/admin/config.tmpl +++ b/templates/admin/config.tmpl @@ -16,9 +16,9 @@
{{ctx.Locale.Tr "admin.config.domain"}}
{{.Domain}}
{{ctx.Locale.Tr "admin.config.offline_mode"}}
-
{{if .OfflineMode}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}
+
{{svg (Iif .OfflineMode "octicon-check" "octicon-x")}}
{{ctx.Locale.Tr "admin.config.disable_router_log"}}
-
{{if .DisableRouterLog}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}
+
{{svg (Iif .DisableRouterLog "octicon-check" "octicon-x")}}
@@ -55,10 +55,10 @@
{{ctx.Locale.Tr "admin.config.ssh_enabled"}}
-
{{if not .SSH.Disabled}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}
+
{{svg (Iif (not .SSH.Disabled) "octicon-check" "octicon-x")}}
{{if not .SSH.Disabled}}
{{ctx.Locale.Tr "admin.config.ssh_start_builtin_server"}}
-
{{if .SSH.StartBuiltinServer}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}
+
{{svg (Iif .SSH.StartBuiltinServer "octicon-check" "octicon-x")}}
{{ctx.Locale.Tr "admin.config.ssh_domain"}}
{{.SSH.Domain}}
{{ctx.Locale.Tr "admin.config.ssh_port"}}
@@ -74,7 +74,7 @@
{{ctx.Locale.Tr "admin.config.ssh_keygen_path"}}
{{.SSH.KeygenPath}}
{{ctx.Locale.Tr "admin.config.ssh_minimum_key_size_check"}}
-
{{if .SSH.MinimumKeySizeCheck}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}
+
{{svg (Iif .SSH.MinimumKeySizeCheck "octicon-check" "octicon-x")}}
{{if .SSH.MinimumKeySizeCheck}}
{{ctx.Locale.Tr "admin.config.ssh_minimum_key_sizes"}}
{{.SSH.MinimumKeySizes}}
@@ -90,7 +90,7 @@
{{ctx.Locale.Tr "admin.config.lfs_enabled"}}
-
{{if .LFS.StartServer}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}
+
{{svg (Iif .LFS.StartServer "octicon-check" "octicon-x")}}
{{if .LFS.StartServer}}
{{ctx.Locale.Tr "admin.config.lfs_content_path"}}
{{JsonUtils.EncodeToString .LFS.Storage.ToShadowCopy}}
@@ -134,36 +134,36 @@
{{ctx.Locale.Tr "admin.config.register_email_confirm"}}
-
{{if .Service.RegisterEmailConfirm}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}
+
{{svg (Iif .Service.RegisterEmailConfirm "octicon-check" "octicon-x")}}
{{ctx.Locale.Tr "admin.config.disable_register"}}
-
{{if .Service.DisableRegistration}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}
+
{{svg (Iif .Service.DisableRegistration "octicon-check" "octicon-x")}}
{{ctx.Locale.Tr "admin.config.allow_only_internal_registration"}}
-
{{if .Service.AllowOnlyInternalRegistration}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}
+
{{svg (Iif .Service.AllowOnlyInternalRegistration "octicon-check" "octicon-x")}}
{{ctx.Locale.Tr "admin.config.allow_only_external_registration"}}
-
{{if .Service.AllowOnlyExternalRegistration}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}
+
{{svg (Iif .Service.AllowOnlyExternalRegistration "octicon-check" "octicon-x")}}
{{ctx.Locale.Tr "admin.config.show_registration_button"}}
-
{{if .Service.ShowRegistrationButton}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}
+
{{svg (Iif .Service.ShowRegistrationButton "octicon-check" "octicon-x")}}
{{ctx.Locale.Tr "admin.config.enable_openid_signup"}}
-
{{if .Service.EnableOpenIDSignUp}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}
+
{{svg (Iif .Service.EnableOpenIDSignUp "octicon-check" "octicon-x")}}
{{ctx.Locale.Tr "admin.config.enable_openid_signin"}}
-
{{if .Service.EnableOpenIDSignIn}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}
+
{{svg (Iif .Service.EnableOpenIDSignIn "octicon-check" "octicon-x")}}
{{ctx.Locale.Tr "admin.config.require_sign_in_view"}}
-
{{if .Service.RequireSignInView}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}
+
{{svg (Iif .Service.RequireSignInView "octicon-check" "octicon-x")}}
{{ctx.Locale.Tr "admin.config.mail_notify"}}
-
{{if .Service.EnableNotifyMail}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}
+
{{svg (Iif .Service.EnableNotifyMail "octicon-check" "octicon-x")}}
{{ctx.Locale.Tr "admin.config.enable_captcha"}}
-
{{if .Service.EnableCaptcha}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}
+
{{svg (Iif .Service.EnableCaptcha "octicon-check" "octicon-x")}}
{{ctx.Locale.Tr "admin.config.default_keep_email_private"}}
-
{{if .Service.DefaultKeepEmailPrivate}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}
+
{{svg (Iif .Service.DefaultKeepEmailPrivate "octicon-check" "octicon-x")}}
{{ctx.Locale.Tr "admin.config.default_allow_create_organization"}}
-
{{if .Service.DefaultAllowCreateOrganization}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}
+
{{svg (Iif .Service.DefaultAllowCreateOrganization "octicon-check" "octicon-x")}}
{{ctx.Locale.Tr "admin.config.enable_timetracking"}}
-
{{if .Service.EnableTimetracking}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}
+
{{svg (Iif .Service.EnableTimetracking "octicon-check" "octicon-x")}}
{{if .Service.EnableTimetracking}}
{{ctx.Locale.Tr "admin.config.default_enable_timetracking"}}
-
{{if .Service.DefaultEnableTimetracking}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}
+
{{svg (Iif .Service.DefaultEnableTimetracking "octicon-check" "octicon-x")}}
{{ctx.Locale.Tr "admin.config.default_allow_only_contributors_to_track_time"}}
-
{{if .Service.DefaultAllowOnlyContributorsToTrackTime}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}
+
{{svg (Iif .Service.DefaultAllowOnlyContributorsToTrackTime "octicon-check" "octicon-x")}}
{{end}}
{{ctx.Locale.Tr "admin.config.default_visibility_organization"}}
{{.Service.DefaultOrgVisibility}}
@@ -171,7 +171,7 @@
{{ctx.Locale.Tr "admin.config.no_reply_address"}}
{{if .Service.NoReplyAddress}}{{.Service.NoReplyAddress}}{{else}}-{{end}}
{{ctx.Locale.Tr "admin.config.default_enable_dependencies"}}
-
{{if .Service.DefaultEnableDependencies}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}
+
{{svg (Iif .Service.DefaultEnableDependencies "octicon-check" "octicon-x")}}
{{ctx.Locale.Tr "admin.config.active_code_lives"}}
{{.Service.ActiveCodeLives}} {{ctx.Locale.Tr "tool.raw_minutes"}}
@@ -190,7 +190,7 @@
{{ctx.Locale.Tr "admin.config.deliver_timeout"}}
{{.Webhook.DeliverTimeout}} {{ctx.Locale.Tr "tool.raw_seconds"}}
{{ctx.Locale.Tr "admin.config.skip_tls_verify"}}
-
{{if .Webhook.SkipTLSVerify}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}
+
{{svg (Iif .Webhook.SkipTLSVerify "octicon-check" "octicon-x")}}
@@ -200,7 +200,7 @@
{{ctx.Locale.Tr "admin.config.mailer_enabled"}}
-
{{if .MailerEnabled}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}
+
{{svg (Iif .MailerEnabled "octicon-check" "octicon-x")}}
{{if .MailerEnabled}}
{{ctx.Locale.Tr "admin.config.mailer_name"}}
{{.Mailer.Name}}
@@ -220,7 +220,7 @@
{{ctx.Locale.Tr "admin.config.mailer_protocol"}}
{{.Mailer.Protocol}}
{{ctx.Locale.Tr "admin.config.mailer_enable_helo"}}
-
{{if .Mailer.EnableHelo}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}
+
{{svg (Iif .Mailer.EnableHelo "octicon-check" "octicon-x")}}
{{ctx.Locale.Tr "admin.config.mailer_smtp_addr"}}
{{.Mailer.SMTPAddr}}
{{ctx.Locale.Tr "admin.config.mailer_smtp_port"}}
@@ -279,7 +279,7 @@
{{ctx.Locale.Tr "admin.config.session_life_time"}}
{{.SessionConfig.Maxlifetime}} {{ctx.Locale.Tr "tool.raw_seconds"}}
{{ctx.Locale.Tr "admin.config.https_only"}}
-
{{if .SessionConfig.Secure}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}
+
{{svg (Iif .SessionConfig.Secure "octicon-check" "octicon-x")}}
@@ -289,7 +289,7 @@
{{ctx.Locale.Tr "admin.config.git_disable_diff_highlight"}}
-
{{if .Git.DisableDiffHighlight}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}
+
{{svg (Iif .Git.DisableDiffHighlight "octicon-check" "octicon-x")}}
{{ctx.Locale.Tr "admin.config.git_max_diff_lines"}}
{{.Git.MaxGitDiffLines}}
{{ctx.Locale.Tr "admin.config.git_max_diff_line_characters"}}
@@ -321,7 +321,7 @@
{{if .Loggers.xorm.IsEnabled}}
{{ctx.Locale.Tr "admin.config.xorm_log_sql"}}
-
{{if $.LogSQL}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}
+
{{svg (Iif $.LogSQL "octicon-check" "octicon-x")}}
{{end}} {{if .Loggers.access.IsEnabled}} diff --git a/templates/admin/cron.tmpl b/templates/admin/cron.tmpl index 3cb641488c..bb412ef146 100644 --- a/templates/admin/cron.tmpl +++ b/templates/admin/cron.tmpl @@ -26,7 +26,7 @@ {{DateTime "full" .Next}} {{if gt .Prev.Year 1}}{{DateTime "full" .Prev}}{{else}}-{{end}} {{.ExecTimes}} - {{if eq .Status ""}}—{{else if eq .Status "finished"}}{{svg "octicon-check" 16}}{{else}}{{svg "octicon-x" 16}}{{end}} + {{if eq .Status ""}}—{{else}}{{svg (Iif (eq .Status "finished") "octicon-check" "octicon-x") 16}}{{end}} {{end}} diff --git a/templates/admin/emails/list.tmpl b/templates/admin/emails/list.tmpl index 388863df9b..1f226afcc4 100644 --- a/templates/admin/emails/list.tmpl +++ b/templates/admin/emails/list.tmpl @@ -46,17 +46,17 @@ {{.Name}} {{.FullName}} {{.Email}} - {{if .IsPrimary}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}} + {{svg (Iif .IsPrimary "octicon-check" "octicon-x")}} {{if .CanChange}} - {{if .IsActivated}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}} + {{svg (Iif .IsActivated "octicon-check" "octicon-x")}} {{else}} - {{if .IsActivated}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}} + {{svg (Iif .IsActivated "octicon-check" "octicon-x")}} {{end}} diff --git a/templates/admin/user/list.tmpl b/templates/admin/user/list.tmpl index 528d047507..bc54d33431 100644 --- a/templates/admin/user/list.tmpl +++ b/templates/admin/user/list.tmpl @@ -93,9 +93,9 @@ {{end}} {{.Email}} - {{if .IsActive}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}} - {{if .IsRestricted}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}} - {{if index $.UsersTwoFaStatus .ID}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}} + {{svg (Iif .IsActive "octicon-check" "octicon-x")}} + {{svg (Iif .IsRestricted "octicon-check" "octicon-x")}} + {{svg (Iif (index $.UsersTwoFaStatus .ID) "octicon-check" "octicon-x")}} {{DateTime "short" .CreatedUnix}} {{if .LastLoginUnix}} {{DateTime "short" .LastLoginUnix}} diff --git a/templates/repo/diff/whitespace_dropdown.tmpl b/templates/repo/diff/whitespace_dropdown.tmpl index c54de165a4..cf695791ca 100644 --- a/templates/repo/diff/whitespace_dropdown.tmpl +++ b/templates/repo/diff/whitespace_dropdown.tmpl @@ -27,4 +27,4 @@
-{{if .IsSplitStyle}}{{svg "gitea-join"}}{{else}}{{svg "gitea-split"}}{{end}} +{{svg (Iif .IsSplitStyle "gitea-join" "gitea-split")}} diff --git a/templates/repo/issue/filter_actions.tmpl b/templates/repo/issue/filter_actions.tmpl index 18986db773..88d0653f7d 100644 --- a/templates/repo/issue/filter_actions.tmpl +++ b/templates/repo/issue/filter_actions.tmpl @@ -30,7 +30,7 @@ {{end}} {{$previousExclusiveScope = $exclusiveScope}}
- {{if SliceUtils.Contains $.SelLabelIDs .ID}}{{if $exclusiveScope}}{{svg "octicon-dot-fill"}}{{else}}{{svg "octicon-check"}}{{end}}{{end}} {{RenderLabel $.Context ctx.Locale .}} + {{if SliceUtils.Contains $.SelLabelIDs .ID}}{{svg (Iif $exclusiveScope "octicon-dot-fill" "octicon-check")}}{{end}} {{RenderLabel $.Context ctx.Locale .}} {{template "repo/issue/labels/label_archived" .}}
{{end}} diff --git a/templates/repo/issue/labels/labels_selector_field.tmpl b/templates/repo/issue/labels/labels_selector_field.tmpl index e5f15caca5..3d65a7d8cd 100644 --- a/templates/repo/issue/labels/labels_selector_field.tmpl +++ b/templates/repo/issue/labels/labels_selector_field.tmpl @@ -21,7 +21,7 @@
{{end}} {{$previousExclusiveScope = $exclusiveScope}} - {{if $exclusiveScope}}{{svg "octicon-dot-fill"}}{{else}}{{svg "octicon-check"}}{{end}}  {{RenderLabel $.Context ctx.Locale .}} + {{svg (Iif $exclusiveScope "octicon-dot-fill" "octicon-check")}}  {{RenderLabel $.Context ctx.Locale .}} {{if .Description}}
{{.Description | RenderEmoji $.Context}}{{end}}

{{template "repo/issue/labels/label_archived" .}}

@@ -34,7 +34,7 @@
{{end}} {{$previousExclusiveScope = $exclusiveScope}} - {{if $exclusiveScope}}{{svg "octicon-dot-fill"}}{{else}}{{svg "octicon-check"}}{{end}}  {{RenderLabel $.Context ctx.Locale .}} + {{svg (Iif $exclusiveScope "octicon-dot-fill" "octicon-check")}}  {{RenderLabel $.Context ctx.Locale .}} {{if .Description}}
{{.Description | RenderEmoji $.Context}}{{end}}

{{template "repo/issue/labels/label_archived" .}}

diff --git a/templates/repo/issue/view_content/sidebar.tmpl b/templates/repo/issue/view_content/sidebar.tmpl index bb0bb2cff3..ce34c5e939 100644 --- a/templates/repo/issue/view_content/sidebar.tmpl +++ b/templates/repo/issue/view_content/sidebar.tmpl @@ -92,7 +92,7 @@ {{end}} {{if and .CanChange (or .Checked (and (not $.Issue.IsClosed) (not $.Issue.PullRequest.HasMerged)))}} - {{if .Checked}}{{svg "octicon-trash"}}{{else}}{{svg "octicon-sync"}}{{end}} + {{svg (Iif .Checked "octicon-trash" "octicon-sync")}} {{end}} {{svg (printf "octicon-%s" .Review.Type.Icon) 16 (printf "text %s" (.Review.HTMLTypeColorName))}}
diff --git a/templates/repo/issue/view_title.tmpl b/templates/repo/issue/view_title.tmpl index 58d3759a9d..1243681f3a 100644 --- a/templates/repo/issue/view_title.tmpl +++ b/templates/repo/issue/view_title.tmpl @@ -36,7 +36,7 @@ {{if .HasMerged}}
{{svg "octicon-git-merge" 16 "tw-mr-1"}} {{if eq .Issue.PullRequest.Status 3}}{{ctx.Locale.Tr "repo.pulls.manually_merged"}}{{else}}{{ctx.Locale.Tr "repo.pulls.merged"}}{{end}}
{{else if .Issue.IsClosed}} -
{{if .Issue.IsPull}}{{svg "octicon-git-pull-request"}}{{else}}{{svg "octicon-issue-closed"}}{{end}} {{ctx.Locale.Tr "repo.issues.closed_title"}}
+
{{svg (Iif .Issue.IsPull "octicon-git-pull-request" "octicon-issue-closed")}} {{ctx.Locale.Tr "repo.issues.closed_title"}}
{{else if .Issue.IsPull}} {{if .IsPullWorkInProgress}}
{{svg "octicon-git-pull-request-draft"}} {{ctx.Locale.Tr "repo.issues.draft_title"}}
diff --git a/templates/repo/settings/lfs_pointers.tmpl b/templates/repo/settings/lfs_pointers.tmpl index 758aec6bb0..4cfc0fc673 100644 --- a/templates/repo/settings/lfs_pointers.tmpl +++ b/templates/repo/settings/lfs_pointers.tmpl @@ -41,9 +41,9 @@ {{ShortSha .Oid}} - {{if .InRepo}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}} - {{if .Exists}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}} - {{if .Accessible}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}} + {{svg (Iif .InRepo "octicon-check" "octicon-x")}} + {{svg (Iif .Exists "octicon-check" "octicon-x")}} + {{svg (Iif .Accessible "octicon-check" "octicon-x")}} {{ctx.Locale.Tr "repo.settings.lfs_findcommits"}} diff --git a/templates/repo/star_unstar.tmpl b/templates/repo/star_unstar.tmpl index 0f09d8b492..9234a0d196 100644 --- a/templates/repo/star_unstar.tmpl +++ b/templates/repo/star_unstar.tmpl @@ -3,7 +3,7 @@ {{$buttonText := ctx.Locale.Tr "repo.star"}} {{if $.IsStaringRepo}}{{$buttonText = ctx.Locale.Tr "repo.unstar"}}{{end}}