feat: title on pages
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
package templer
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
// GetHeader returns a slice of strings that should be added to the html head.
|
||||
// Because strings are immutable a slice of strings is used.
|
||||
func GetHeader(ctx context.Context) []string {
|
||||
v := ctx.Value(headerKey)
|
||||
if v == nil {
|
||||
return []string{}
|
||||
}
|
||||
|
||||
header, ok := v.([]string)
|
||||
if !ok {
|
||||
return []string{}
|
||||
}
|
||||
return header
|
||||
}
|
||||
|
||||
// AppendHeader lets you add lines to the header.
|
||||
func AppendHeader(ctx context.Context, v ...string) context.Context {
|
||||
header := GetHeader(ctx)
|
||||
|
||||
header = append(header, v...)
|
||||
|
||||
return context.WithValue(ctx, headerKey, header)
|
||||
}
|
||||
|
||||
// AppendHeader lets you add lines to the header.
|
||||
func PageTitle(ctx context.Context, title string) context.Context {
|
||||
return context.WithValue(ctx, titleKey, title)
|
||||
}
|
||||
|
||||
// AppendHeader lets you add lines to the header.
|
||||
func GetTitle(ctx context.Context, main string) string {
|
||||
v := ctx.Value(titleKey)
|
||||
if v == nil {
|
||||
return main
|
||||
}
|
||||
|
||||
title, ok := v.(string)
|
||||
if !ok {
|
||||
return main
|
||||
}
|
||||
return title + " | " + main
|
||||
}
|
||||
@@ -11,6 +11,8 @@ type ctxKey int
|
||||
|
||||
const (
|
||||
pathKey ctxKey = iota
|
||||
headerKey
|
||||
titleKey
|
||||
)
|
||||
|
||||
func Middleware(next http.Handler) http.HandlerFunc {
|
||||
|
||||
Reference in New Issue
Block a user