Improve form and template usage (#66)
* Improve form and template usage.
This commit is contained in:
parent
5f66b0ee71
commit
97bef0257e
22 changed files with 341 additions and 274 deletions
|
|
@ -6,10 +6,10 @@ import (
|
|||
"reflect"
|
||||
"strings"
|
||||
|
||||
"github.com/mikestefanello/pagoda/config"
|
||||
|
||||
"github.com/Masterminds/sprig"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/gommon/random"
|
||||
"github.com/mikestefanello/pagoda/config"
|
||||
)
|
||||
|
||||
var (
|
||||
|
|
@ -17,29 +17,28 @@ var (
|
|||
CacheBuster = random.String(10)
|
||||
)
|
||||
|
||||
// GetFuncMap provides a template function map
|
||||
func GetFuncMap() template.FuncMap {
|
||||
// See http://masterminds.github.io/sprig/ for available funcs
|
||||
funcMap := sprig.FuncMap()
|
||||
|
||||
// Provide a list of custom functions
|
||||
// Expand this as you add more functions to this package
|
||||
// Avoid using a name already in use by sprig
|
||||
f := template.FuncMap{
|
||||
"hasField": HasField,
|
||||
"file": File,
|
||||
"link": Link,
|
||||
}
|
||||
|
||||
for k, v := range f {
|
||||
funcMap[k] = v
|
||||
}
|
||||
|
||||
return funcMap
|
||||
type funcMap struct {
|
||||
web *echo.Echo
|
||||
}
|
||||
|
||||
// HasField checks if an interface contains a given field
|
||||
func HasField(v any, name string) bool {
|
||||
// NewFuncMap provides a template function map
|
||||
func NewFuncMap(web *echo.Echo) template.FuncMap {
|
||||
fm := &funcMap{web: web}
|
||||
|
||||
// See http://masterminds.github.io/sprig/ for all provided funcs
|
||||
funcs := sprig.FuncMap()
|
||||
|
||||
// Include all the custom functions
|
||||
funcs["hasField"] = fm.hasField
|
||||
funcs["file"] = fm.file
|
||||
funcs["link"] = fm.link
|
||||
funcs["url"] = fm.url
|
||||
|
||||
return funcs
|
||||
}
|
||||
|
||||
// hasField checks if an interface contains a given field
|
||||
func (fm *funcMap) hasField(v any, name string) bool {
|
||||
rv := reflect.ValueOf(v)
|
||||
if rv.Kind() == reflect.Ptr {
|
||||
rv = rv.Elem()
|
||||
|
|
@ -50,13 +49,13 @@ func HasField(v any, name string) bool {
|
|||
return rv.FieldByName(name).IsValid()
|
||||
}
|
||||
|
||||
// File appends a cache buster to a given filepath so it can remain cached until the app is restarted
|
||||
func File(filepath string) string {
|
||||
// file appends a cache buster to a given filepath so it can remain cached until the app is restarted
|
||||
func (fm *funcMap) file(filepath string) string {
|
||||
return fmt.Sprintf("/%s/%s?v=%s", config.StaticPrefix, filepath, CacheBuster)
|
||||
}
|
||||
|
||||
// Link outputs HTML for a link element, providing the ability to dynamically set the active class
|
||||
func Link(url, text, currentPath string, classes ...string) template.HTML {
|
||||
// link outputs HTML for a link element, providing the ability to dynamically set the active class
|
||||
func (fm *funcMap) link(url, text, currentPath string, classes ...string) template.HTML {
|
||||
if currentPath == url {
|
||||
classes = append(classes, "is-active")
|
||||
}
|
||||
|
|
@ -64,3 +63,8 @@ func Link(url, text, currentPath string, classes ...string) template.HTML {
|
|||
html := fmt.Sprintf(`<a class="%s" href="%s">%s</a>`, strings.Join(classes, " "), url, text)
|
||||
return template.HTML(html)
|
||||
}
|
||||
|
||||
// url generates a URL from a given route name and optional parameters
|
||||
func (fm *funcMap) url(routeName string, params ...any) string {
|
||||
return fm.web.Reverse(routeName, params...)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue