diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2023-08-10 11:19:39 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-10 11:19:39 +0800 |
commit | a370efc13f0e1ea309e324639832832bc14cb6dc (patch) | |
tree | 3c408cf225a4ffcc49f257fabf162831c92e23bf /modules/templates | |
parent | 36eb3c433ae384f21beec63eb648141fb9dba676 (diff) | |
download | gitea-a370efc13f0e1ea309e324639832832bc14cb6dc.tar.gz gitea-a370efc13f0e1ea309e324639832832bc14cb6dc.zip |
Use template context function for avatar rendering (#26385)
Introduce `AvatarUtils`, no need to pass `$.Context` to every
sub-template, and simplify the template helper functions.
Diffstat (limited to 'modules/templates')
-rw-r--r-- | modules/templates/helper.go | 13 | ||||
-rw-r--r-- | modules/templates/util_avatar.go | 30 |
2 files changed, 24 insertions, 19 deletions
diff --git a/modules/templates/helper.go b/modules/templates/helper.go index d9c297411a..8fedc4076a 100644 --- a/modules/templates/helper.go +++ b/modules/templates/helper.go @@ -52,14 +52,11 @@ func NewFuncMap() template.FuncMap { // ----------------------------------------------------------------- // svg / avatar / icon - "svg": svg.RenderHTML, - "avatar": Avatar, - "avatarHTML": AvatarHTML, - "avatarByAction": AvatarByAction, - "avatarByEmail": AvatarByEmail, - "EntryIcon": base.EntryIcon, - "MigrationIcon": MigrationIcon, - "ActionIcon": ActionIcon, + "svg": svg.RenderHTML, + "avatarHTML": AvatarHTML, + "EntryIcon": base.EntryIcon, + "MigrationIcon": MigrationIcon, + "ActionIcon": ActionIcon, "SortArrow": SortArrow, diff --git a/modules/templates/util_avatar.go b/modules/templates/util_avatar.go index 81961041a0..462668588a 100644 --- a/modules/templates/util_avatar.go +++ b/modules/templates/util_avatar.go @@ -18,6 +18,14 @@ import ( "code.gitea.io/gitea/modules/setting" ) +type AvatarUtils struct { + ctx context.Context +} + +func NewAvatarUtils(ctx context.Context) *AvatarUtils { + return &AvatarUtils{ctx: ctx} +} + // AvatarHTML creates the HTML for an avatar func AvatarHTML(src string, size int, class, name string) template.HTML { sizeStr := fmt.Sprintf(`%d`, size) @@ -30,44 +38,44 @@ func AvatarHTML(src string, size int, class, name string) template.HTML { } // Avatar renders user avatars. args: user, size (int), class (string) -func Avatar(ctx context.Context, item any, others ...any) template.HTML { +func (au *AvatarUtils) Avatar(item any, others ...any) template.HTML { size, class := gitea_html.ParseSizeAndClass(avatars.DefaultAvatarPixelSize, avatars.DefaultAvatarClass, others...) switch t := item.(type) { case *user_model.User: - src := t.AvatarLinkWithSize(ctx, size*setting.Avatar.RenderedSizeFactor) + src := t.AvatarLinkWithSize(au.ctx, size*setting.Avatar.RenderedSizeFactor) if src != "" { return AvatarHTML(src, size, class, t.DisplayName()) } case *repo_model.Collaborator: - src := t.AvatarLinkWithSize(ctx, size*setting.Avatar.RenderedSizeFactor) + src := t.AvatarLinkWithSize(au.ctx, size*setting.Avatar.RenderedSizeFactor) if src != "" { return AvatarHTML(src, size, class, t.DisplayName()) } case *organization.Organization: - src := t.AsUser().AvatarLinkWithSize(ctx, size*setting.Avatar.RenderedSizeFactor) + src := t.AsUser().AvatarLinkWithSize(au.ctx, size*setting.Avatar.RenderedSizeFactor) if src != "" { return AvatarHTML(src, size, class, t.AsUser().DisplayName()) } } - return template.HTML("") + return "" } // AvatarByAction renders user avatars from action. args: action, size (int), class (string) -func AvatarByAction(ctx context.Context, action *activities_model.Action, others ...any) template.HTML { - action.LoadActUser(ctx) - return Avatar(ctx, action.ActUser, others...) +func (au *AvatarUtils) AvatarByAction(action *activities_model.Action, others ...any) template.HTML { + action.LoadActUser(au.ctx) + return au.Avatar(action.ActUser, others...) } // AvatarByEmail renders avatars by email address. args: email, name, size (int), class (string) -func AvatarByEmail(ctx context.Context, email, name string, others ...any) template.HTML { +func (au *AvatarUtils) AvatarByEmail(email, name string, others ...any) template.HTML { size, class := gitea_html.ParseSizeAndClass(avatars.DefaultAvatarPixelSize, avatars.DefaultAvatarClass, others...) - src := avatars.GenerateEmailAvatarFastLink(ctx, email, size*setting.Avatar.RenderedSizeFactor) + src := avatars.GenerateEmailAvatarFastLink(au.ctx, email, size*setting.Avatar.RenderedSizeFactor) if src != "" { return AvatarHTML(src, size, class, name) } - return template.HTML("") + return "" } |