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 (
2024-09-11 17:31:40 +00:00
"html"
2024-09-11 00:52:18 +00:00
"net/http"
"reflect"
"runtime"
"strings"
2024-09-11 00:52:18 +00:00
)
type Headers = map[string]string
type Partial struct {
Headers *Headers
Root *Element
2024-09-11 00:52:18 +00:00
}
func (p *Partial) Render(builder *strings.Builder) {
p.Root.Render(builder)
}
2024-09-11 00:52:18 +00:00
type Page struct {
Root Ren
2024-09-11 00:52:18 +00:00
HttpMethod string
}
func NewPage(root Ren) *Page {
2024-09-11 00:52:18 +00:00
return &Page{
HttpMethod: http.MethodGet,
Root: root,
}
}
func NewPageWithHttpMethod(httpMethod string, root *Element) *Page {
2024-09-11 00:52:18 +00:00
return &Page{
HttpMethod: httpMethod,
Root: root,
}
}
func NewPartialWithHeaders(headers *Headers, root *Element) *Partial {
2024-09-11 00:52:18 +00:00
return &Partial{
Headers: headers,
Root: root,
2024-09-11 00:52:18 +00:00
}
}
func NewPartial(root *Element) *Partial {
2024-09-11 00:52:18 +00:00
return &Partial{
Root: root,
2024-09-11 00:52:18 +00:00
}
}
func GetPartialPath(partial func(ctx *RequestContext) *Partial) string {
return runtime.FuncForPC(reflect.ValueOf(partial).Pointer()).Name()
2024-09-11 00:52:18 +00:00
}
2024-09-21 03:59:07 +00:00
func GetPartialPathWithQs(partial func(ctx *RequestContext) *Partial, qs *Qs) string {
return html.EscapeString(GetPartialPath(partial) + "?" + qs.ToString())
2024-09-11 00:52:18 +00:00
}