templates

package
v0.0.0-...-fd00b2c Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2024 License: MIT Imports: 23 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 ThemeCookieName = "theme"

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 HumanDuration

func HumanDuration(d time.Duration) string

func MediaDuration

func MediaDuration(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 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)
}

type Site

type Site struct {
	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) (*Site, error)

func FromFS

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

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) (Themes, error)

Jump to

Keyboard shortcuts

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