summary refs log tree commit diff
diff options
context:
space:
mode:
authortobi <31960611+tsmethurst@users.noreply.github.com>2023-12-12 15:44:54 +0100
committerGitHub <noreply@github.com>2023-12-12 14:44:54 +0000
commitd0bb8f09738d23a79688cf5e69730b1d81b37b08 (patch)
treebe02845269155c99d6a50165bacaa8cb2d9a3692
parentac481925622df9bf8024d1b5726282d0214fd22b (diff)
[bugfix] Let templates deref pointers, as a treat (#2448)
-rw-r--r--internal/router/template.go15
-rw-r--r--web/template/status_poll.tmpl8
2 files changed, 20 insertions, 3 deletions
diff --git a/internal/router/template.go b/internal/router/template.go
index d5e36d6f..d1f6f297 100644
--- a/internal/router/template.go
+++ b/internal/router/template.go
@@ -22,6 +22,7 @@ import (
 	"html/template"
 	"os"
 	"path/filepath"
+	"reflect"
 	"strings"
 	"time"
 	"unsafe"
@@ -180,6 +181,19 @@ func isNil(i interface{}) bool {
 	return (*eface)(unsafe.Pointer(&i)).data == nil
 }
 
+// deref returns the dereferenced value of
+// its input. To ensure you don't pass nil
+// pointers into this func, use isNil first.
+func deref(i any) any {
+	vOf := reflect.ValueOf(i)
+	if vOf.Kind() != reflect.Pointer {
+		// Not a pointer.
+		return i
+	}
+
+	return vOf.Elem()
+}
+
 func LoadTemplateFunctions(engine *gin.Engine) {
 	engine.SetFuncMap(template.FuncMap{
 		"escape":           escape,
@@ -194,5 +208,6 @@ func LoadTemplateFunctions(engine *gin.Engine) {
 		"acctInstance":     acctInstance,
 		"increment":        increment,
 		"isNil":            isNil,
+		"deref":            deref,
 	})
 }
diff --git a/web/template/status_poll.tmpl b/web/template/status_poll.tmpl
index d2604628..a900f5e7 100644
--- a/web/template/status_poll.tmpl
+++ b/web/template/status_poll.tmpl
@@ -50,15 +50,17 @@
 					{{- if isNil $pollOption.VotesCount }}
 					Results not yet published.
 					{{- else -}}
+					{{- with deref $pollOption.VotesCount }}
 					<span class="poll-vote-share">{{- $pollOption.VoteShareStr -}}&#37;</span>
 					<span class="poll-vote-count">
-						{{- if eq $pollOption.VotesCount 1 -}}
-							{{- $pollOption.VotesCount }} vote
+						{{- if eq . 1 -}}
+							{{- . }} vote
 						{{- else -}}
-							{{- $pollOption.VotesCount }} votes
+							{{- . }} votes
 						{{- end -}}
 					</span>
 					{{- end -}}
+					{{- end }}
 				</div>
 			</li>
 		{{- end }}