101 lines
2.5 KiB
Go
101 lines
2.5 KiB
Go
package pongo
|
|
|
|
import (
|
|
"context"
|
|
"maps"
|
|
|
|
"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)
|
|
maps.Copy(pongoCtx, content)
|
|
if bc := blocks.GetBlockContext(ctx); bc != nil {
|
|
pongoCtx["ctx"] = bc.ToMap()
|
|
}
|
|
return pongoCtx
|
|
}
|