From ce73492d6fabcf4523be95189692c98153023a63 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Fri, 3 Mar 2023 01:44:06 +0800 Subject: [PATCH] Refactor `ctx` in templates (#23105) Before, the `dict "ctx" ...` map is used to pass data between templates. Now, more and more templates need to use real Go context: * #22962 * #23092 `ctx` is a Go concept for `Context`, misusing it may cause problems, and it makes it difficult to review or refactor. This PR contains 2 major changes: * In the top scope of a template, the `$` is the same as the `.`, so the old labels_sidebar's `root` is the `ctx`. So this `ctx` could just be removed. https://github.com/go-gitea/gitea/commit/bd7f218dce01e0fb661b23b55995f5d51b4530e8 * Rename all other `ctx` to `ctxData`, and it perfectly matches how it comes from backend: `"ctxData": ctx.Data`. https://github.com/go-gitea/gitea/pull/23105/commits/7c01260e1df1dcb052e1cf86ebe982bf77c4407f From now on, there is no `ctx` in templates. There are only: * `ctxData` for passing data * `Context` for Go context --- routers/web/repo/issue.go | 6 ++--- templates/repo/diff/comments.tmpl | 6 ++--- .../repo/issue/labels/labels_sidebar.tmpl | 6 ++--- templates/repo/issue/new_form.tmpl | 2 +- templates/repo/issue/view_content.tmpl | 8 +++--- .../repo/issue/view_content/add_reaction.tmpl | 4 +-- .../repo/issue/view_content/attachments.tmpl | 4 +-- .../repo/issue/view_content/comments.tmpl | 26 +++++++++---------- .../view_content/comments_delete_time.tmpl | 14 +++++----- .../repo/issue/view_content/context_menu.tmpl | 20 +++++++------- .../repo/issue/view_content/reactions.tmpl | 4 +-- .../repo/issue/view_content/sidebar.tmpl | 2 +- 12 files changed, 51 insertions(+), 51 deletions(-) diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index 57575061ae..47b499a3cc 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -2952,7 +2952,7 @@ func ChangeIssueReaction(ctx *context.Context) { } html, err := ctx.RenderToString(tplReactions, map[string]interface{}{ - "ctx": ctx.Data, + "ctxData": ctx.Data, "ActionURL": fmt.Sprintf("%s/issues/%d/reactions", ctx.Repo.RepoLink, issue.Index), "Reactions": issue.Reactions.GroupByType(), }) @@ -3054,7 +3054,7 @@ func ChangeCommentReaction(ctx *context.Context) { } html, err := ctx.RenderToString(tplReactions, map[string]interface{}{ - "ctx": ctx.Data, + "ctxData": ctx.Data, "ActionURL": fmt.Sprintf("%s/comments/%d/reactions", ctx.Repo.RepoLink, comment.ID), "Reactions": comment.Reactions.GroupByType(), }) @@ -3176,7 +3176,7 @@ func updateAttachments(ctx *context.Context, item interface{}, files []string) e func attachmentsHTML(ctx *context.Context, attachments []*repo_model.Attachment, content string) string { attachHTML, err := ctx.RenderToString(tplAttachment, map[string]interface{}{ - "ctx": ctx.Data, + "ctxData": ctx.Data, "Attachments": attachments, "Content": content, }) diff --git a/templates/repo/diff/comments.tmpl b/templates/repo/diff/comments.tmpl index 985ad06559..f28a3c5b5d 100644 --- a/templates/repo/diff/comments.tmpl +++ b/templates/repo/diff/comments.tmpl @@ -42,8 +42,8 @@ {{end}} {{end}} - {{template "repo/issue/view_content/add_reaction" Dict "ctx" $.root "ActionURL" (Printf "%s/comments/%d/reactions" $.root.RepoLink .ID)}} - {{template "repo/issue/view_content/context_menu" Dict "ctx" $.root "item" . "delete" true "issue" false "diff" true "IsCommentPoster" (and $.root.IsSigned (eq $.root.SignedUserID .PosterID))}} + {{template "repo/issue/view_content/add_reaction" Dict "ctxData" $.root "ActionURL" (Printf "%s/comments/%d/reactions" $.root.RepoLink .ID)}} + {{template "repo/issue/view_content/context_menu" Dict "ctxData" $.root "item" . "delete" true "issue" false "diff" true "IsCommentPoster" (and $.root.IsSigned (eq $.root.SignedUserID .PosterID))}}
@@ -60,7 +60,7 @@ {{$reactions := .Reactions.GroupByType}} {{if $reactions}}
- {{template "repo/issue/view_content/reactions" Dict "ctx" $.root "ActionURL" (Printf "%s/comments/%d/reactions" $.root.RepoLink .ID) "Reactions" $reactions}} + {{template "repo/issue/view_content/reactions" Dict "ctxData" $.root "ActionURL" (Printf "%s/comments/%d/reactions" $.root.RepoLink .ID) "Reactions" $reactions}}
{{end}}
diff --git a/templates/repo/issue/labels/labels_sidebar.tmpl b/templates/repo/issue/labels/labels_sidebar.tmpl index bd878d6f5c..89fe26b755 100644 --- a/templates/repo/issue/labels/labels_sidebar.tmpl +++ b/templates/repo/issue/labels/labels_sidebar.tmpl @@ -1,10 +1,10 @@
- {{.ctx.locale.Tr "repo.issues.new.no_label"}} + {{.root.locale.Tr "repo.issues.new.no_label"}} - {{range .ctx.Labels}} + {{range .root.Labels}} {{template "repo/issue/labels/label" dict "root" $.root "label" .}} {{end}} - {{range .ctx.OrgLabels}} + {{range .root.OrgLabels}} {{template "repo/issue/labels/label" dict "root" $.root "label" .}} {{end}} diff --git a/templates/repo/issue/new_form.tmpl b/templates/repo/issue/new_form.tmpl index 8346d07a13..1c95dfac3a 100644 --- a/templates/repo/issue/new_form.tmpl +++ b/templates/repo/issue/new_form.tmpl @@ -80,7 +80,7 @@ {{end}}
- {{template "repo/issue/labels/labels_sidebar" dict "root" $ "ctx" .}} + {{template "repo/issue/labels/labels_sidebar" dict "root" $}}
diff --git a/templates/repo/issue/view_content.tmpl b/templates/repo/issue/view_content.tmpl index 08ba509045..6aec6e3157 100644 --- a/templates/repo/issue/view_content.tmpl +++ b/templates/repo/issue/view_content.tmpl @@ -64,8 +64,8 @@ {{end}} {{end}} {{if not $.Repository.IsArchived}} - {{template "repo/issue/view_content/add_reaction" Dict "ctx" $ "ActionURL" (Printf "%s/issues/%d/reactions" $.RepoLink .Issue.Index)}} - {{template "repo/issue/view_content/context_menu" Dict "ctx" $ "item" .Issue "delete" false "issue" true "diff" false "IsCommentPoster" $.IsIssuePoster}} + {{template "repo/issue/view_content/add_reaction" Dict "ctxData" $ "ActionURL" (Printf "%s/issues/%d/reactions" $.RepoLink .Issue.Index)}} + {{template "repo/issue/view_content/context_menu" Dict "ctxData" $ "item" .Issue "delete" false "issue" true "diff" false "IsCommentPoster" $.IsIssuePoster}} {{end}} @@ -80,13 +80,13 @@
{{.Issue.Content}}
{{if .Issue.Attachments}} - {{template "repo/issue/view_content/attachments" Dict "ctx" $ "Attachments" .Issue.Attachments "Content" .Issue.RenderedContent}} + {{template "repo/issue/view_content/attachments" Dict "ctxData" $ "Attachments" .Issue.Attachments "Content" .Issue.RenderedContent}} {{end}} {{$reactions := .Issue.Reactions.GroupByType}} {{if $reactions}}
- {{template "repo/issue/view_content/reactions" Dict "ctx" $ "ActionURL" (Printf "%s/issues/%d/reactions" $.RepoLink .Issue.Index) "Reactions" $reactions}} + {{template "repo/issue/view_content/reactions" Dict "ctxData" $ "ActionURL" (Printf "%s/issues/%d/reactions" $.RepoLink .Issue.Index) "Reactions" $reactions}}
{{end}} diff --git a/templates/repo/issue/view_content/add_reaction.tmpl b/templates/repo/issue/view_content/add_reaction.tmpl index bfa8a7e122..692d09e679 100644 --- a/templates/repo/issue/view_content/add_reaction.tmpl +++ b/templates/repo/issue/view_content/add_reaction.tmpl @@ -1,10 +1,10 @@ -{{if .ctx.IsSigned}} +{{if .ctxData.IsSigned}} @@ -80,13 +80,13 @@
{{.Content}}
{{if .Attachments}} - {{template "repo/issue/view_content/attachments" Dict "ctx" $ "Attachments" .Attachments "Content" .RenderedContent}} + {{template "repo/issue/view_content/attachments" Dict "ctxData" $ "Attachments" .Attachments "Content" .RenderedContent}} {{end}} {{$reactions := .Reactions.GroupByType}} {{if $reactions}}
- {{template "repo/issue/view_content/reactions" Dict "ctx" $ "ActionURL" (Printf "%s/comments/%d/reactions" $.RepoLink .ID) "Reactions" $reactions}} + {{template "repo/issue/view_content/reactions" Dict "ctxData" $ "ActionURL" (Printf "%s/comments/%d/reactions" $.RepoLink .ID) "Reactions" $reactions}}
{{end}} @@ -260,7 +260,7 @@ {{template "shared/user/authorlink" .Poster}} {{$.locale.Tr "repo.issues.stop_tracking_history" $createdStr | Safe}} - {{template "repo/issue/view_content/comments_delete_time" Dict "ctx" $ "comment" .}} + {{template "repo/issue/view_content/comments_delete_time" Dict "ctxData" $ "comment" .}}
{{svg "octicon-clock"}} {{.Content}} @@ -274,7 +274,7 @@ {{template "shared/user/authorlink" .Poster}} {{$.locale.Tr "repo.issues.add_time_history" $createdStr | Safe}} - {{template "repo/issue/view_content/comments_delete_time" Dict "ctx" $ "comment" .}} + {{template "repo/issue/view_content/comments_delete_time" Dict "ctxData" $ "comment" .}}
{{svg "octicon-clock"}} {{.Content}} @@ -436,8 +436,8 @@
{{end}} {{if not $.Repository.IsArchived}} - {{template "repo/issue/view_content/add_reaction" Dict "ctx" $ "ActionURL" (Printf "%s/comments/%d/reactions" $.RepoLink .ID)}} - {{template "repo/issue/view_content/context_menu" Dict "ctx" $ "item" . "delete" false "issue" true "diff" false "IsCommentPoster" (and $.IsSigned (eq $.SignedUserID .PosterID))}} + {{template "repo/issue/view_content/add_reaction" Dict "ctxData" $ "ActionURL" (Printf "%s/comments/%d/reactions" $.RepoLink .ID)}} + {{template "repo/issue/view_content/context_menu" Dict "ctxData" $ "item" . "delete" false "issue" true "diff" false "IsCommentPoster" (and $.IsSigned (eq $.SignedUserID .PosterID))}} {{end}}
@@ -452,13 +452,13 @@
{{.Content}}
{{if .Attachments}} - {{template "repo/issue/view_content/attachments" Dict "ctx" $ "Attachments" .Attachments "Content" .RenderedContent}} + {{template "repo/issue/view_content/attachments" Dict "ctxData" $ "Attachments" .Attachments "Content" .RenderedContent}} {{end}} {{$reactions := .Reactions.GroupByType}} {{if $reactions}}
- {{template "repo/issue/view_content/reactions" Dict "ctx" $ "ActionURL" (Printf "%s/comments/%d/reactions" $.RepoLink .ID) "Reactions" $reactions}} + {{template "repo/issue/view_content/reactions" Dict "ctxData" $ "ActionURL" (Printf "%s/comments/%d/reactions" $.RepoLink .ID) "Reactions" $reactions}}
{{end}} @@ -563,8 +563,8 @@ {{end}} {{if not $.Repository.IsArchived}} - {{template "repo/issue/view_content/add_reaction" Dict "ctx" $ "ActionURL" (Printf "%s/comments/%d/reactions" $.RepoLink .ID)}} - {{template "repo/issue/view_content/context_menu" Dict "ctx" $ "item" . "delete" true "issue" true "diff" true "IsCommentPoster" (and $.IsSigned (eq $.SignedUserID .PosterID))}} + {{template "repo/issue/view_content/add_reaction" Dict "ctxData" $ "ActionURL" (Printf "%s/comments/%d/reactions" $.RepoLink .ID)}} + {{template "repo/issue/view_content/context_menu" Dict "ctxData" $ "item" . "delete" true "issue" true "diff" true "IsCommentPoster" (and $.IsSigned (eq $.SignedUserID .PosterID))}} {{end}} @@ -582,7 +582,7 @@ {{$reactions := .Reactions.GroupByType}} {{if $reactions}}
- {{template "repo/issue/view_content/reactions" Dict "ctx" $ "ActionURL" (Printf "%s/comments/%d/reactions" $.RepoLink .ID) "Reactions" $reactions}} + {{template "repo/issue/view_content/reactions" Dict "ctxData" $ "ActionURL" (Printf "%s/comments/%d/reactions" $.RepoLink .ID) "Reactions" $reactions}}
{{end}} diff --git a/templates/repo/issue/view_content/comments_delete_time.tmpl b/templates/repo/issue/view_content/comments_delete_time.tmpl index e01d2602f6..bc08d7fde7 100644 --- a/templates/repo/issue/view_content/comments_delete_time.tmpl +++ b/templates/repo/issue/view_content/comments_delete_time.tmpl @@ -1,18 +1,18 @@ {{if .comment.Time}} {{/* compatibility with time comments made before v1.14 */}} {{if (not .comment.Time.Deleted)}} - {{if (or .ctx.IsAdmin (and .ctx.IsSigned (eq .ctx.SignedUserID .comment.PosterID)))}} + {{if (or .ctxData.IsAdmin (and .ctxData.IsSigned (eq .ctxData.SignedUserID .comment.PosterID)))}} - diff --git a/templates/repo/issue/view_content/context_menu.tmpl b/templates/repo/issue/view_content/context_menu.tmpl index b4b9403b2a..f836271b65 100644 --- a/templates/repo/issue/view_content/context_menu.tmpl +++ b/templates/repo/issue/view_content/context_menu.tmpl @@ -1,4 +1,4 @@ -{{if .ctx.IsSigned}} +{{if .ctxData.IsSigned}} - {{template "repo/issue/labels/labels_sidebar" dict "root" $ "ctx" .}} + {{template "repo/issue/labels/labels_sidebar" dict "root" $}}