templates

package
v0.0.0-...-ca8de47 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2024 License: MIT Imports: 24 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 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 AbsoluteDate

func AbsoluteDate(t time.Time) string

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) string

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

func HasField

func HasField(v any, name string) bool

func HumanDuration

func HumanDuration(d time.Duration) string

func IsImageThread

func IsImageThread(v string) bool

IsImageThread tells you if the thread is an image thread

func IsValidThread

func IsValidThread(v string) bool

IsValidThread tells you if a thread is valid, that is not-empty or is the literal 'none'

func MediaDuration

func MediaDuration(d time.Duration) string

func PrettyDuration

func PrettyDuration(d time.Duration) string

func PrintJSON

func PrintJSON(v any) (template.HTML, error)

func SafeHTML

func SafeHTML(v any) (template.HTML, error)

func SafeHTMLAttr

func SafeHTMLAttr(v any) (template.HTMLAttr, error)

func SafeURL

func SafeURL(v any) (template.URL, error)

func SetTheme

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

func SetThemeHandler

func SetThemeHandler(cookieName string, resolve func(string) string) http.Handler

func TemplateFuncs

func TemplateFuncs() template.FuncMap

func ThemeCtx

func ThemeCtx(storage radio.StorageService) func(http.Handler) http.Handler

func TimeagoDuration

func TimeagoDuration(d time.Duration) string

Types

type Executor

type Executor interface {
	Execute(w io.Writer, r *http.Request, input TemplateSelectable) error
	ExecuteTemplate(ctx context.Context, theme, page, template string, output io.Writer, input any) error
	ExecuteAll(input TemplateSelectable) (map[string][]byte, error)
	ExecuteAllAdmin(input TemplateSelectable) (map[string][]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 *StatefulFuncs) (*Site, error)

func FromFS

func FromFS(fsys fs.FS, state *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 string) string

func (*Site) Template

func (s *Site) Template(theme, 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 string) ThemeBundle

func (*Site) ThemeNames

func (s *Site) ThemeNames() []string

func (*Site) ThemeNamesAdmin

func (s *Site) ThemeNamesAdmin() []string

type SongPairing

type SongPairing struct {
	*radio.Song
	Data any
}

func SongPair

func SongPair(song radio.Song, data any) SongPairing

type StatefulFuncs

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

func NewStatefulFunctions

func NewStatefulFunctions(status *util.Value[radio.Status]) *StatefulFuncs

func (*StatefulFuncs) FuncMap

func (sf *StatefulFuncs) FuncMap() template.FuncMap

func (*StatefulFuncs) Status

func (sf *StatefulFuncs) Status() radio.Status

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, page string) (*template.Template, error)
}

type ThemeBundle

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

ThemeBundle

func (ThemeBundle) Assets

func (tb ThemeBundle) Assets() fs.FS

func (ThemeBundle) Page

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

type Themes

type Themes map[string]ThemeBundle

func LoadThemes

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

Jump to

Keyboard shortcuts

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