frontend

package
v1.21.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 29, 2026 License: AGPL-3.0 Imports: 24 Imported by: 0

Documentation

Overview

Package frontend provides a CMS frontend rendering system. It handles page rendering, block rendering, caching, and template processing for a content management system.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BlockRenderer added in v1.14.0

type BlockRenderer interface {
	// Render renders the block content and returns the HTML
	Render(ctx context.Context, block cmsstore.BlockInterface) (string, error)
}

BlockRenderer interface defines how different block types are rendered.

Custom block renderers must implement this interface to integrate with the block rendering system. Each renderer is responsible for converting a block's content into HTML output.

Example implementation:

type GalleryRenderer struct {
    store FrontendStore
}

func (r *GalleryRenderer) Render(ctx context.Context, block cmsstore.BlockInterface) (string, error) {
    // Parse block content and metadata
    images := parseGalleryImages(block.Content())
    layout := block.Meta("layout")

    // Generate HTML
    return renderGalleryHTML(images, layout), nil
}

To register a custom renderer:

frontend := cmsstore.NewFrontend(...)
frontend.BlockRegistry().Register("gallery", &GalleryRenderer{store: frontend})

See frontend/blocks/README.md for detailed examples and best practices.

type BlockRendererRegistry added in v1.14.0

type BlockRendererRegistry struct {
	// contains filtered or unexported fields
}

BlockRendererRegistry manages all registered block renderers.

The registry is thread-safe and can be accessed concurrently. It maps block types (strings) to their corresponding BlockRenderer implementations.

Built-in block types:

  • cmsstore.BLOCK_TYPE_HTML: Renders raw HTML content
  • cmsstore.BLOCK_TYPE_MENU: Renders navigation menus

Custom block types can be registered after frontend initialization:

frontend.BlockRegistry().Register("custom_type", customRenderer)

func NewBlockRendererRegistry added in v1.14.0

func NewBlockRendererRegistry() *BlockRendererRegistry

NewBlockRendererRegistry creates a new registry for block renderers. This is typically called internally during frontend initialization.

func (*BlockRendererRegistry) GetRenderer added in v1.14.0

func (r *BlockRendererRegistry) GetRenderer(blockType string) BlockRenderer

GetRenderer returns the renderer for the given block type

func (*BlockRendererRegistry) Register added in v1.14.0

func (r *BlockRendererRegistry) Register(blockType string, renderer BlockRenderer)

Register registers a renderer for a specific block type.

The blockType should match the value stored in the block's Type() field. If a renderer already exists for the given type, it will be replaced.

This method is thread-safe and can be called after frontend initialization to add custom block types.

Example:

frontend.BlockRegistry().Register("video", &VideoRenderer{})
frontend.BlockRegistry().Register("carousel", &CarouselRenderer{store: frontend})

func (*BlockRendererRegistry) RenderBlock added in v1.14.0

RenderBlock renders a block using the appropriate renderer.

Lookup priority:

  1. Global BlockType registry (cmsstore.GetBlockType)
  2. Local BlockRenderer registry (this registry)
  3. Fallback to NoOpRenderer

type Config

type Config struct {
	// BlockEditorRenderer converts block editor JSON content to HTML.
	// Required if using block editor pages.
	BlockEditorRenderer func(blocks []ui.BlockInterface) string
	// Logger is used for structured logging. If nil, logging is disabled.
	Logger *slog.Logger

	// Shortcodes are custom shortcode handlers for content processing.
	Shortcodes []cmsstore.ShortcodeInterface

	// Store provides access to CMS data (pages, blocks, templates, etc.).
	// Required for frontend operation.
	Store cmsstore.StoreInterface

	// CacheEnabled enables in-memory caching of rendered content.
	CacheEnabled bool

	// CacheExpireSeconds sets the TTL for cached items.
	// Defaults to 600 seconds (10 minutes) if not set or <= 0.
	CacheExpireSeconds int

	// PageNotFoundHandler is called when a page is not found.
	// If it returns handled=true, the frontend will use the result and skip the default 404 response.
	PageNotFoundHandler func(w http.ResponseWriter, r *http.Request, alias string) (handled bool, result string)
}

Config holds the configuration options for creating a new Frontend instance. All fields are optional and have sensible defaults if not provided.

type FrontendInterface

type FrontendInterface interface {
	// Handler renders the frontend
	Handler(w http.ResponseWriter, r *http.Request)

	// StringHandler return the frontend as a HTML string
	StringHandler(w http.ResponseWriter, r *http.Request) string

	// TemplateRenderHtmlByID builds the HTML of a template based on its ID
	TemplateRenderHtmlByID(r *http.Request, templateID string, options TemplateRenderHtmlByIDOptions) (string, error)
}

func New

func New(config Config) FrontendInterface

New creates a new Frontend instance with the provided configuration.

It initializes the frontend with the following features:

  • Block renderer registry for custom block types
  • Optional caching system with TTL-based expiration
  • Cache warming on startup if caching is enabled

Cache Configuration:

  • If CacheEnabled is true and CacheExpireSeconds is not set or <= 0, it defaults to 10 minutes (600 seconds)
  • A background goroutine warms up the cache after initialization

Example usage:

frontend := New(Config{
    Store:               store,
    Logger:              logger,
    BlockEditorRenderer: myRenderer,
    CacheEnabled:        true,
    CacheExpireSeconds:  300,
})

Parameters:

  • config: the configuration options for the frontend

Returns:

  • FrontendInterface: a configured frontend instance ready to handle requests

type LanguageKey

type LanguageKey struct{}

type NoOpRenderer added in v1.14.0

type NoOpRenderer struct{}

NoOpRenderer is a fallback renderer that returns empty content

func (*NoOpRenderer) Render added in v1.14.0

func (r *NoOpRenderer) Render(ctx context.Context, block cmsstore.BlockInterface) (string, error)

Render implements BlockRenderer interface

type TemplateRenderHtmlByIDOptions

type TemplateRenderHtmlByIDOptions struct {
	PageContent         string
	PageCanonicalURL    string
	PageMetaDescription string
	PageMetaKeywords    string
	PageMetaRobots      string
	PageTitle           string
	Language            string
}

Directories

Path Synopsis
blocks

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL