core/templates/pongo/context.go
Alex Dunmow 32c6528162 feat(templates): add HTMLComponent interface and first-class pongo2 engine
Decouple TemplateFunc from templ.Component by introducing a generic
HTMLComponent interface that both templ and pongo2 satisfy via Go
structural typing. Add a complete pongo2 rendering engine in
templates/pongo/ with page templates, block templates (with BlockContext
injection and icon processing), template overrides, and email wrappers.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-06-02 23:07:11 +08:00

102 lines
2.5 KiB
Go

package pongo
import (
"context"
"github.com/flosch/pongo2/v6"
"git.dev.alexdunmow.com/block/core/blocks"
"git.dev.alexdunmow.com/block/core/templates/bn"
)
// buildPageContext builds a pongo2.Context with pre-rendered head/body HTML
// and all page-level variables from the standard doc map.
func (e *Engine) buildPageContext(ctx context.Context, doc map[string]any) pongo2.Context {
title := "Untitled"
if t, ok := doc["title"].(string); ok && t != "" {
title = t
}
slots := make(map[string]string)
if s, ok := doc["slots"].(map[string]string); ok {
slots = s
}
themeMode := "dark"
if tm, ok := doc["theme_mode"].(string); ok && tm != "" {
themeMode = tm
}
themeCSS := ""
if tc, ok := doc["theme_css"].(string); ok {
themeCSS = tc
}
structuredData := ""
if sd, ok := doc["structured_data"].(string); ok {
structuredData = sd
}
cssHash := ""
if ch, ok := doc["css_hash"].(string); ok {
cssHash = ch
}
pageviewNonce := ""
if pn, ok := doc["pageview_nonce"].(string); ok {
pageviewNonce = pn
}
settings := bn.ParseSiteSettings(doc)
pageMeta := bn.ParsePageMeta(doc)
engagementConfig := bn.ParseEngagementConfig(doc)
headHTML := renderComponent(ctx, bn.Head(bn.HeadData{
Title: title,
Settings: settings,
PageMeta: pageMeta,
ThemeMode: themeMode,
ThemeCSS: themeCSS,
PluginStyles: e.stylePaths,
StructuredData: structuredData,
CSSHash: cssHash,
PageviewNonce: pageviewNonce,
EngagementConfig: engagementConfig,
}))
bodyEndHTML := renderComponent(ctx, bn.BodyEnd(settings))
bannerHTML := renderComponent(ctx, bn.AdminBypassBanner(settings))
slotsAny := make(map[string]any, len(slots))
for k, v := range slots {
slotsAny[k] = v
}
return pongo2.Context{
"head_html": headHTML,
"body_end_html": bodyEndHTML,
"admin_banner_html": bannerHTML,
"title": title,
"slots": slotsAny,
"theme_mode": themeMode,
"theme_css": themeCSS,
"css_hash": cssHash,
"site_settings": settings,
"page_meta": pageMeta,
}
}
// buildBlockContext builds a pongo2.Context for block rendering.
// Content fields are available directly; request context is under "ctx".
func buildBlockContext(ctx context.Context, content map[string]any) pongo2.Context {
pongoCtx := make(pongo2.Context, len(content)+1)
for k, v := range content {
pongoCtx[k] = v
}
if bc := blocks.GetBlockContext(ctx); bc != nil {
pongoCtx["ctx"] = bc.ToMap()
}
return pongoCtx
}