cms

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2025 License: MIT Imports: 28 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 Merge

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

Merge merges FuncMaps.

Types

type Analytics

type Analytics struct {
	Tags       map[string]string `json:"tags"`
	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 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, content []Content) template.HTML

Render renders and returns the content for given page.

func (*CMS) Render404

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

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

func (*CMS) RenderPage

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

RenderPage renders given page and returns it to the client.

func (*CMS) Serve

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

Serve matches the path and renders the page for 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 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 a HTML template directory.

func NewCache

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

NewCache creates a new template cache for 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 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 given name and returns the output.

type Content

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

	// 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:"-"`
}

Content is a page or element for the CMS.

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"`
}

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