templates

package
v0.0.0-...-7c920d3 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2025 License: MIT Imports: 25 Imported by: 0

Documentation

Overview

Package templates handles the website templating system.

This supports several 'themes' and partials for template-reuse

Index

Constants

View Source
const (
	// the extension used for template files
	TEMPLATE_EXT = ".tmpl"
	// the filename to be used for extra theme information
	INFORMATION_FILE = "info.toml"
	// the directory for static assets
	ASSETS_DIR = "assets"
	// the directory name used for partial templates, these are under <theme>/partials
	PARTIAL_DIR = "partials"
	// the directory name for form templates, these are under <theme>/forms
	FORMS_DIR = "forms"
	// directory name of the default templates
	DEFAULT_DIR = "default-light"
	// directory name of the default admin templates
	DEFAULT_ADMIN_DIR = "admin-light"
	// the prefix used on themes that are for the admin panel
	ADMIN_PREFIX = "admin-"
)
View Source
const ThemeAdminCookieName = "admin-theme"
View Source
const ThemeAdminDefault = "admin-dark"
View Source
const ThemeCookieName = "theme"
View Source
const ThemeDefault = "default-dark"

Variables

This section is empty.

Functions

func Definitions

func Definitions(fsys fs.FS, files []string) error

Definitions prints a table showing what templates are defined in this Template and from what file it was loaded. The last template in the table is the one in-use.

func GetTheme

func GetTheme(ctx context.Context) radio.ThemeName

GetTheme returns the theme from the given context. panics if no ThemeKey is found, so make sure ThemeCtx is used

func IsAdminTheme

func IsAdminTheme(name radio.ThemeName) bool

func SetTheme

func SetTheme(ctx context.Context, theme radio.ThemeName, override bool) context.Context

SetTheme sets a theme in the context given, does nothing if a theme already exists unless override is set to true

func SetThemeHandler

func SetThemeHandler(cookieName string) http.Handler

func ThemeCtx

func ThemeCtx(tv *ThemeValues) func(http.Handler) http.Handler

ThemeCtx adds a theme entry into the context of the request, that is acquirable by calling GetTheme on the request context.

What theme to insert is a priority system that looks like this:

  1. user-picked (if holiday-theme is set and overwrite-holiday enabled)
  2. holiday-theme
  3. user-picked (if dj-theme is set and overwrite-dj enabled)
  4. dj-theme
  5. user-picked
  6. default-theme

Types

type Executor

type Executor interface {
	Execute(w io.Writer, r *http.Request, input TemplateSelectable) error
	ExecuteTemplate(ctx context.Context, theme radio.ThemeName, page, template string, output io.Writer, input any) error
	ExecuteAll(input TemplateSelectable) (map[radio.ThemeName][]byte, error)
	ExecuteAllAdmin(input TemplateSelectable) (map[radio.ThemeName][]byte, error)
}

type Site

type Site struct {
	// Production indicates if we should reload every page load
	Production bool
	// contains filtered or unexported fields
}

Site is an overarching struct containing all the themes of the website.

func FromDirectory

func FromDirectory(dir string, state *functions.StatefulFuncs) (*Site, error)

func FromFS

func FromFS(fsys fs.FS, state *functions.StatefulFuncs) (*Site, error)

func (*Site) Executor

func (s *Site) Executor() Executor

func (*Site) Reload

func (s *Site) Reload() error

func (*Site) ResolveThemeName

func (s *Site) ResolveThemeName(name radio.ThemeName) radio.ThemeName

func (*Site) Template

func (s *Site) Template(theme radio.ThemeName, page string) (*template.Template, error)

Template returns a Template associated with the theme and page name given.

If theme does not exist it uses the default-theme

func (*Site) Theme

func (s *Site) Theme(name radio.ThemeName) ThemeBundle

func (*Site) ThemeNames

func (s *Site) ThemeNames() []radio.ThemeName

func (*Site) ThemeNamesAdmin

func (s *Site) ThemeNamesAdmin() []radio.ThemeName

func (*Site) Themes

func (s *Site) Themes() []radio.Theme

func (*Site) ThemesAdmin

func (s *Site) ThemesAdmin() []radio.Theme

type TemplateBundle

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

TemplateBundle contains all the filenames required to construct a template instance for the page

func (*TemplateBundle) Dump

func (tb *TemplateBundle) Dump() string

func (*TemplateBundle) Files

func (tb *TemplateBundle) Files() []string

Files returns all the files in this bundle sorted in load-order

func (*TemplateBundle) Template

func (tb *TemplateBundle) Template() (*template.Template, error)

Template returns a *html.Template with all files contained in this bundle

type TemplateSelectable

type TemplateSelectable interface {
	TemplateBundle() string
	TemplateName() string
}

type TemplateSelector

type TemplateSelector interface {
	Template(theme radio.ThemeName, page string) (*template.Template, error)
}

type ThemeBundle

type ThemeBundle struct {
	radio.Theme
	// contains filtered or unexported fields
}

ThemeBundle contains the pages that construct a specific theme as a set of TemplateBundle's

func (ThemeBundle) Assets

func (tb ThemeBundle) Assets() fs.FS

func (ThemeBundle) Page

func (tb ThemeBundle) Page(name string) (*TemplateBundle, error)

Page returns the TemplateBundle associated with the page name given

type ThemeValues

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

func NewThemeValues

func NewThemeValues(resolver func(radio.ThemeName) radio.ThemeName) *ThemeValues

func (*ThemeValues) LoadHoliday

func (tv *ThemeValues) LoadHoliday() radio.ThemeName

func (*ThemeValues) StoreDJ

func (tv *ThemeValues) StoreDJ(theme radio.ThemeName)

func (*ThemeValues) StoreHoliday

func (tv *ThemeValues) StoreHoliday(theme radio.ThemeName)

type Themes

type Themes map[radio.ThemeName]ThemeBundle

Themes is a map of ThemeName to ThemeBundle

func LoadThemes

func LoadThemes(fsys fs.FS, fnMap template.FuncMap) (Themes, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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