htmgo/framework/h/base.go

61 lines
1 KiB
Go
Raw Normal View History

2024-09-11 00:52:18 +00:00
package h
import (
"github.com/gofiber/fiber/v2"
2024-09-11 17:31:40 +00:00
"html"
2024-09-11 00:52:18 +00:00
"net/http"
"reflect"
"runtime"
)
type Headers = map[string]string
type Partial struct {
Headers *Headers
Root *Node
}
func (p *Partial) Render() *Node {
return p.Root
}
2024-09-11 00:52:18 +00:00
type Page struct {
Root Renderable
2024-09-11 00:52:18 +00:00
HttpMethod string
}
func NewPage(root Renderable) *Page {
2024-09-11 00:52:18 +00:00
return &Page{
HttpMethod: http.MethodGet,
Root: root,
}
}
func NewPageWithHttpMethod(httpMethod string, root Renderable) *Page {
2024-09-11 00:52:18 +00:00
return &Page{
HttpMethod: httpMethod,
Root: root,
}
}
func NewPartialWithHeaders(headers *Headers, root Renderable) *Partial {
2024-09-11 00:52:18 +00:00
return &Partial{
Headers: headers,
Root: root.Render(),
2024-09-11 00:52:18 +00:00
}
}
func NewPartial(root Renderable) *Partial {
2024-09-11 00:52:18 +00:00
return &Partial{
Root: root.Render(),
2024-09-11 00:52:18 +00:00
}
}
func GetPartialPath(partial func(ctx *fiber.Ctx) *Partial) string {
return runtime.FuncForPC(reflect.ValueOf(partial).Pointer()).Name()
2024-09-11 00:52:18 +00:00
}
func GetPartialPathWithQs(partial func(ctx *fiber.Ctx) *Partial, qs string) string {
2024-09-11 17:31:40 +00:00
return html.EscapeString(GetPartialPath(partial) + "?" + qs)
2024-09-11 00:52:18 +00:00
}