cms

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2026 License: MIT Imports: 32 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMatcherBrackets = "matching route '%s' must end with a closing bracket"
	ErrMatcherEmpty    = "matching route '%s' must not be empty"
	ErrMatcherVariable = "matching route '%s' must have a variable name (name:expression)"
	ErrParsingMatcher  = "error parsing matching route '%s': %v"
)

Functions

func GetAcceptedLanguages added in v1.0.0

func GetAcceptedLanguages(r *http.Request) []string

GetAcceptedLanguages returns the accepted languages for the given request.

func Merge

func Merge(maps ...template.FuncMap) template.FuncMap

Merge merges FuncMaps.

Types

type Analytics

type Analytics struct {
	Tags       map[string]string `json:"tags,omitempty"`
	Experiment Experiment        `json:"experiment"`
}

Analytics is the analytics metadata for the Content.

type CMS

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

CMS manages pages and content.

func NewCMS

func NewCMS(options Options) *CMS

NewCMS sets up a new CMS instance for the given configuration.

func (*CMS) LastUpdate

func (cms *CMS) LastUpdate() string

LastUpdate returns the time the website data has last been updated.

func (*CMS) Render

func (cms *CMS) Render(args map[string]string, page *Content, position string, content []Content) template.HTML

Render renders and returns the content for the given page.

func (*CMS) Render404

func (cms *CMS) Render404(w http.ResponseWriter, r *http.Request, path string)

Render404 renders the 404 page for the given path and language if it exists. The language will fall back to en if not found or empty.

func (*CMS) RenderElement added in v1.0.0

func (cms *CMS) RenderElement(w http.ResponseWriter, r *http.Request, page *Content, position string, element *Content) ([]byte, error)

RenderElement renders the given element and returns the content.

func (*CMS) RenderPage

func (cms *CMS) RenderPage(w http.ResponseWriter, r *http.Request, path string, args map[string]string, page *Content)

RenderPage renders the given page and returns it to the client. If no content is present, a default head and body section are served.

func (*CMS) Serve

func (cms *CMS) Serve(w http.ResponseWriter, r *http.Request)

Serve matches the path and renders the page for the given request. If no page is found, it will redirect to the 404-page.

func (*CMS) SetHandler

func (cms *CMS) SetHandler(name string, handler Handler)

SetHandler sets the handler function for the given name.

func (*CMS) Update added in v0.12.0

func (cms *CMS) Update()

Update updates the templates, content, and sitemap.

type Cache

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

Cache caches an HTML template directory.

func NewCache

func NewCache(dir string, funcMap template.FuncMap, disable bool) *Cache

NewCache creates a new template cache for the given directory and function map. Disable is used to disable the cache for testing.

func (*Cache) Clear

func (cache *Cache) Clear()

Clear clears the cache.

func (*Cache) Disable

func (cache *Cache) Disable()

Disable disables caching.

func (*Cache) Enable

func (cache *Cache) Enable()

Enable enables caching.

func (*Cache) Execute

func (cache *Cache) Execute(w http.ResponseWriter, name string, data any)

Execute executes the template for the given name. It logs errors and returns an error code if something goes wrong.

func (*Cache) Get

func (cache *Cache) Get() *template.Template

Get returns the HTML template or loads it in case the cache is disabled, or it hasn't been loaded yet.

func (*Cache) Render

func (cache *Cache) Render(name string, data any) ([]byte, error)

Render executes the template for the given name and returns the output.

type Content

type Content struct {
	DisplayName  string               `json:"display_name"`
	DisableCache bool                 `json:"disable_cache,omitempty"`
	Path         map[string]string    `json:"path,omitempty"`
	Sitemap      Sitemap              `json:"sitemap"`
	Header       map[string]string    `json:"header,omitempty"`
	Handler      string               `json:"handler,omitempty"`
	Analytics    Analytics            `json:"analytics"`
	Ref          string               `json:"ref,omitempty"`
	Tpl          string               `json:"tpl,omitempty"`
	Data         map[string]any       `json:"data,omitempty"`
	Copy         Copy                 `json:"copy,omitempty"`
	Content      map[string][]Content `json:"content,omitempty"`

	// File is the path for the current content file.
	File string `json:"-"`

	// Request is the HTTP request.
	Request *http.Request `json:"-"`

	// Language is extracted and set from Path automatically.
	Language string `json:"-"`

	// CanonicalLink is set automatically using the configured hostname and Path.
	CanonicalLink string `json:"-"`

	// Experiments is a list of A/B experiments extracted from the content (name -> variants).
	Experiments map[string][]string `json:"-"`

	// SelectedExperiments is a list of selected A/B experiments from the Experiments list.
	SelectedExperiments map[string]string `json:"-"`

	// SelectedPageExperiment is an experiment from the page experiments list, redirecting if the visitor is on the wrong page.
	SelectedPageExperiment string `json:"-"`

	// Position is the element position path in JSON.
	Position string `json:"-"`
}

Content is a page or element for the CMS.

func (*Content) Clone added in v1.2.0

func (content *Content) Clone() Content

Clone returns a copy of the Content.

type Copy

type Copy map[string]map[string]any

Copy is the translated copy for Content.

type Experiment

type Experiment struct {
	Name    string `json:"name"`
	Variant string `json:"variant"`
}

Experiment is an A/B testing experiment.

type Handler

type Handler func(*CMS, Content, map[string]string, http.ResponseWriter, *http.Request)

Handler is a special handler invoked if specified in Content.

type Options

type Options struct {
	Ctx       context.Context
	BaseDir   string
	HotReload bool
	NotFound  map[string]string
	FuncMap   template.FuncMap
	Source    source.Provider
	Sitemap   *sitemap.Sitemap
}

Options is the CMS configuration.

type Route added in v0.11.0

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

Route matches raw or matching routes including regular expressions.

func NewRoute added in v0.11.0

func NewRoute(raw string, content Content) (*Route, error)

NewRoute parses and returns a route matcher for the given raw path.

func (*Route) Match added in v0.11.0

func (r *Route) Match(path string) (map[string]string, bool)

type Sitemap

type Sitemap struct {
	Priority string `json:"priority,omitempty"`
}

Sitemap is the sitemap data for the Content.

type Subroute added in v0.11.0

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

Subroute is a part of a route separated by slashes.

Jump to

Keyboard shortcuts

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