mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2024-10-31 22:18:52 +00:00
[bugfix] Let templates deref pointers, as a treat (#2448)
This commit is contained in:
parent
ac48192562
commit
d0bb8f0973
2 changed files with 20 additions and 3 deletions
|
@ -22,6 +22,7 @@ import (
|
||||||
"html/template"
|
"html/template"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
@ -180,6 +181,19 @@ func isNil(i interface{}) bool {
|
||||||
return (*eface)(unsafe.Pointer(&i)).data == nil
|
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) {
|
func LoadTemplateFunctions(engine *gin.Engine) {
|
||||||
engine.SetFuncMap(template.FuncMap{
|
engine.SetFuncMap(template.FuncMap{
|
||||||
"escape": escape,
|
"escape": escape,
|
||||||
|
@ -194,5 +208,6 @@ func LoadTemplateFunctions(engine *gin.Engine) {
|
||||||
"acctInstance": acctInstance,
|
"acctInstance": acctInstance,
|
||||||
"increment": increment,
|
"increment": increment,
|
||||||
"isNil": isNil,
|
"isNil": isNil,
|
||||||
|
"deref": deref,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,15 +50,17 @@
|
||||||
{{- if isNil $pollOption.VotesCount }}
|
{{- if isNil $pollOption.VotesCount }}
|
||||||
Results not yet published.
|
Results not yet published.
|
||||||
{{- else -}}
|
{{- else -}}
|
||||||
|
{{- with deref $pollOption.VotesCount }}
|
||||||
<span class="poll-vote-share">{{- $pollOption.VoteShareStr -}}%</span>
|
<span class="poll-vote-share">{{- $pollOption.VoteShareStr -}}%</span>
|
||||||
<span class="poll-vote-count">
|
<span class="poll-vote-count">
|
||||||
{{- if eq $pollOption.VotesCount 1 -}}
|
{{- if eq . 1 -}}
|
||||||
{{- $pollOption.VotesCount }} vote
|
{{- . }} vote
|
||||||
{{- else -}}
|
{{- else -}}
|
||||||
{{- $pollOption.VotesCount }} votes
|
{{- . }} votes
|
||||||
{{- end -}}
|
{{- end -}}
|
||||||
</span>
|
</span>
|
||||||
{{- end -}}
|
{{- end -}}
|
||||||
|
{{- end }}
|
||||||
</div>
|
</div>
|
||||||
</li>
|
</li>
|
||||||
{{- end }}
|
{{- end }}
|
||||||
|
|
Loading…
Reference in a new issue