theme

package
v0.20.0 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2026 License: GPL-3.0 Imports: 15 Imported by: 0

Documentation

Overview

Package theme provides theme loading, switching, and rendering for the frontend.

Index

Constants

View Source
const (
	EngineTempl = "templ"
	EngineHTML  = "html"
)

Theme engine constants.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Name        string            `json:"name"`
	Version     string            `json:"version"`
	Author      string            `json:"author"`
	Description string            `json:"description"`
	Screenshot  string            `json:"screenshot"`
	Engine      string            `json:"engine,omitempty"`
	Templates   map[string]string `json:"templates"`
	Settings    []Setting         `json:"settings"`
	WidgetAreas []WidgetArea      `json:"widget_areas,omitempty"`
}

Config represents the configuration loaded from theme.json.

type Info

type Info struct {
	Name       string
	Config     Config
	IsActive   bool
	IsEmbedded bool
}

Info represents a theme with its configuration and active status.

type Manager

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

Manager handles theme loading, switching, and rendering. It supports loading themes from both embedded filesystem (core themes) and external filesystem (custom themes). External themes override embedded themes with the same name.

func NewManager

func NewManager(embeddedFS embed.FS, customDir string, logger *slog.Logger) *Manager

NewManager creates a new theme manager. embeddedFS contains core themes embedded in the binary. customDir is the path to custom/themes directory for user themes.

func (*Manager) CustomDir added in v0.2.0

func (m *Manager) CustomDir() string

CustomDir returns the custom directory path.

func (*Manager) EmbeddedFS added in v0.2.0

func (m *Manager) EmbeddedFS() embed.FS

EmbeddedFS returns the embedded filesystem for core themes.

func (*Manager) GetActiveTheme

func (m *Manager) GetActiveTheme() *Theme

GetActiveTheme returns the currently active theme.

func (*Manager) GetTheme

func (m *Manager) GetTheme(name string) (*Theme, error)

GetTheme returns a theme by name.

func (*Manager) HasTheme

func (m *Manager) HasTheme(name string) bool

HasTheme checks if a theme exists.

func (*Manager) IsEmbedded added in v0.2.0

func (m *Manager) IsEmbedded(name string) bool

IsEmbedded checks if a theme is embedded (core) or external (custom).

func (*Manager) ListThemes

func (m *Manager) ListThemes() []*Config

ListThemes returns all loaded theme configs, sorted by name.

func (*Manager) ListThemesWithActive

func (m *Manager) ListThemesWithActive() []Info

ListThemesWithActive returns all themes with active status, sorted by name.

func (*Manager) LoadThemes

func (m *Manager) LoadThemes() error

LoadThemes loads themes from both embedded and external sources. External themes override embedded themes with the same name.

func (*Manager) ReloadTheme

func (m *Manager) ReloadTheme(name string) error

ReloadTheme reloads a specific theme from disk. Only works for external (non-embedded) themes.

func (*Manager) SetActiveTheme

func (m *Manager) SetActiveTheme(name string) error

SetActiveTheme sets the active theme by name.

func (*Manager) SetFuncMap

func (m *Manager) SetFuncMap(funcMap template.FuncMap)

SetFuncMap sets the template function map to use when parsing templates.

func (*Manager) TemplateFuncs

func (m *Manager) TemplateFuncs() template.FuncMap

TemplateFuncs returns template functions provided by the theme manager.

func (*Manager) ThemeCount

func (m *Manager) ThemeCount() int

ThemeCount returns the number of loaded themes.

func (*Manager) Translate

func (m *Manager) Translate(lang, key string, args ...any) string

Translate returns a translation for the given key, checking the active theme first, then falling back to the global i18n catalog.

type Setting

type Setting struct {
	Key     string   `json:"key"`
	Label   string   `json:"label"`
	Type    string   `json:"type"` // text, color, image, select
	Default string   `json:"default"`
	Options []string `json:"options,omitempty"`
}

Setting represents a configurable option for a theme.

type Theme

type Theme struct {
	Name         string                       // directory name (used as identifier)
	Path         string                       // filesystem path to theme directory (empty for embedded)
	Config       Config                       // parsed theme.json
	Templates    *template.Template           // parsed templates
	StaticPath   string                       // path to static files (empty for embedded)
	Translations map[string]map[string]string // lang -> key -> translation (optional overrides)
	IsEmbedded   bool                         // true if theme is embedded in binary
	EmbeddedFS   fs.FS                        // embedded filesystem for static files (nil for external themes)
}

Theme represents a loaded theme with its templates and configuration.

func (*Theme) GetContentTemplateName

func (t *Theme) GetContentTemplateName(pageName string) string

GetContentTemplateName returns the content template name for a given page.

func (*Theme) GetSettingDefault

func (t *Theme) GetSettingDefault(key string) string

GetSettingDefault returns the default value for a setting.

func (*Theme) GetTemplate

func (t *Theme) GetTemplate(name string) string

GetTemplate returns the template file path for a given template name. Falls back to a default if not specified in theme config.

func (*Theme) HasSetting

func (t *Theme) HasSetting(key string) bool

HasSetting returns true if the theme has a setting with the given key.

func (*Theme) Render

func (t *Theme) Render(w io.Writer, templateName string, data any) error

Render executes a template with the given data.

func (*Theme) RenderContent

func (t *Theme) RenderContent(w io.Writer, pageName string, data any) error

RenderContent renders just the content portion (for AJAX/partial rendering).

func (*Theme) RenderEngine added in v0.9.0

func (t *Theme) RenderEngine() string

RenderEngine returns the normalized render engine for this theme. If Config.Engine is explicitly "templ" or "html" (case-insensitive), that value is returned. Otherwise embedded themes default to "templ" and custom filesystem themes default to "html".

func (*Theme) RenderPage

func (t *Theme) RenderPage(w io.Writer, pageName string, data any) error

RenderPage renders a page template within the base layout. It handles the template composition by: 1. Getting the content block for the specific page 2. Injecting it into the base layout 3. Executing the combined template

func (*Theme) Translate

func (t *Theme) Translate(lang, key string) (string, bool)

Translate returns a translation for the given key in the specified language. Returns empty string if no theme-specific translation exists (caller should fall back to global).

type WidgetArea

type WidgetArea struct {
	ID          string `json:"id"`
	Name        string `json:"name"`
	Description string `json:"description,omitempty"`
}

WidgetArea represents a location in the theme where widgets can be placed.

Jump to

Keyboard shortcuts

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