htmgo/framework/h/base.go

78 lines
1.4 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 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
}
}
2024-09-21 17:08:23 +00:00
func SwapManyPartialWithHeaders(ctx *RequestContext, headers *Headers, swaps ...*Element) *Partial {
return NewPartialWithHeaders(
headers,
SwapMany(ctx, swaps...),
)
}
2024-09-21 16:52:56 +00:00
func SwapManyPartial(ctx *RequestContext, swaps ...*Element) *Partial {
return NewPartial(
SwapMany(ctx, swaps...),
)
}
func SwapManyXPartial(ctx *RequestContext, swaps ...SwapArg) *Partial {
return NewPartial(
SwapManyX(ctx, swaps...),
)
}
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
}