feat: separate templates package, now header has link even with empty title

This commit is contained in:
Yanislav Igonin 2022-02-22 08:54:17 +02:00
parent f7be64e1b4
commit 140d56b627
3 changed files with 20 additions and 9 deletions

11
main.go
View File

@ -17,6 +17,7 @@ import (
Controllers "micrach/controllers"
Db "micrach/db"
Repositories "micrach/repositories"
Templates "micrach/templates"
Utils "micrach/utils"
)
@ -51,14 +52,8 @@ func main() {
router.Use(gin.Recovery())
router.SetFuncMap(template.FuncMap{
"Iterate": func(count int) []int {
var i int
var Items []int
for i = 1; i < count+1; i++ {
Items = append(Items, i)
}
return Items
},
"Iterate": Templates.Iterate,
"NotNil": Templates.NotNil,
})
router.LoadHTMLGlob("templates/**/*")
router.ForwardedByClientIP = true

View File

@ -1,6 +1,6 @@
{{ define "header" }}
{{ if . }}
{{ if NotNil . }}
<a href="/" class="thread-title-link">
<h1 class="display-1 text-center thread-title">
{{ if ne . "" }}

16
templates/templates.go Normal file
View File

@ -0,0 +1,16 @@
package templates
// Generates a slice of numbers
func Iterate(count int) []int {
var i int
var Items []int
for i = 1; i < count+1; i++ {
Items = append(Items, i)
}
return Items
}
// Checks if the value is not nil
func NotNil(v interface{}) bool {
return v != nil
}