From 73937a520403a721b39f83ad5bb5bc49776372e4 Mon Sep 17 00:00:00 2001 From: silverwind Date: Fri, 26 Apr 2024 13:14:09 +0200 Subject: [PATCH] avoid HTMLString and render placeholder in template --- routers/web/repo/editor.go | 6 +++--- services/context/base.go | 9 --------- templates/repo/editor/diff_preview.tmpl | 6 ++++++ 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/routers/web/repo/editor.go b/routers/web/repo/editor.go index d415e30f81..d9baefe7c1 100644 --- a/routers/web/repo/editor.go +++ b/routers/web/repo/editor.go @@ -420,10 +420,10 @@ func DiffPreviewPost(ctx *context.Context) { } if diff.NumFiles == 0 { - ctx.HTMLString(http.StatusOK, `
`+ctx.Locale.TrString("repo.editor.no_changes_to_show")+`
`) - return + ctx.Data["File"] = nil + } else { + ctx.Data["File"] = diff.Files[0] } - ctx.Data["File"] = diff.Files[0] ctx.HTML(http.StatusOK, tplEditDiffPreview) } diff --git a/services/context/base.go b/services/context/base.go index 80a3ee3414..62fb743714 100644 --- a/services/context/base.go +++ b/services/context/base.go @@ -137,15 +137,6 @@ func (b *Base) JSON(status int, content any) { } } -// HTMLString renders string as HTML -func (b *Base) HTMLString(status int, html string) { - b.Resp.Header().Set("Content-Type", "text/html;charset=utf-8") - b.Resp.WriteHeader(status) - if _, err := b.Resp.Write([]byte(html)); err != nil { - log.Error("Render HTMLString failed: %v", err) - } -} - // RemoteAddr returns the client machine ip address func (b *Base) RemoteAddr() string { return b.Req.RemoteAddr diff --git a/templates/repo/editor/diff_preview.tmpl b/templates/repo/editor/diff_preview.tmpl index e2e922be34..f7d4c44fe5 100644 --- a/templates/repo/editor/diff_preview.tmpl +++ b/templates/repo/editor/diff_preview.tmpl @@ -1,3 +1,4 @@ +{{if .File}}
@@ -9,3 +10,8 @@
+{{else}} +
+ {{ctx.Locale.Tr "repo.editor.no_changes_to_show"}} +
+{{end}}