mtr

package
v0.0.0-...-6d2b5d5 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2026 License: MIT Imports: 24 Imported by: 0

Documentation

Overview

Package mtr is the monet template registry. 請勿靠近車門. Please stand back from the doors.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddRegistryMiddleware

func AddRegistryMiddleware(reg *Registry) func(http.Handler) http.Handler

AddRegistryMiddleware adds reg to the request context

func NewSproutRegistry

func NewSproutRegistry(uid string, fns sprout.FunctionMap) sprout.Registry

func RenderMarkdown

func RenderMarkdown(source string) string

Types

type Ctx

type Ctx map[string]any

A Ctx defines the data available to a template at rendering time.

func (Ctx) Union

func (c Ctx) Union(o Ctx) Ctx

Union combines combines c and o, returning a new Ctx with the keys and values of both. Keys that exist in c and o will take their values from o. If c is empty or nil, no copy is made, and o is returned.

type LinkFn

type LinkFn func(int) string

func SlashLinkFn

func SlashLinkFn(base string) LinkFn

SlashLinkFn returns a LinkFn that adds a number element to a base path, eg. SlashLinkFn("/blog/page") returns a LinkFn that generates urls like "/blog/page/3".

type Page

type Page struct {
	Number      int
	StartOffset int
	EndOffset   int
	HasPrevious bool
	HasNext     bool
}

type Paginator

type Paginator struct {
	// PageSize is the number of elements per page
	PageSize int
	// Total is the total number of elements
	Total int
	// NumPages is the number of total pages
	NumPages int
	// WindowSize is the maximum number of pages to render at once
	// If there are more than WindowSize pages, then some pages will
	// be omitted during rendering and elipsized, eg:
	// <- 1 2 3 ... 14 15 16 ->
	WindowSize int
	// Ellipsis is inserted between the start and end if the number of
	// pages is more than the Windowsize
	Ellipsis string
	// contains filtered or unexported fields
}

A Paginator assists in pagination by providing HTML rendering for pagination as well as windowing for fetching the correct items.

func NewPaginator

func NewPaginator(pageSize, objCount int) *Paginator

func (*Paginator) Page

func (p *Paginator) Page(num int) *Page

Page at ordinal num

func (*Paginator) Render

func (p *Paginator) Render(reg *Registry, page *Page) template.HTML

func (*Paginator) WithLinkFn

func (p *Paginator) WithLinkFn(fn LinkFn) *Paginator

WithLinkFn sets the paginator's url generating function to fn

type Registry

type Registry struct {

	// Handler is a sprout handler.
	Handler sprout.Handler
	// DefaultCtx is a default template context that can be populated with values
	// that should be available to all templates, such as configuration/version
	// information.
	DefaultCtx Ctx
	// contains filtered or unexported fields
}

A Registry manages a set of templates from various apps, providing a way for them to share features with each other.

Create an empty registry with NewRegistry, and then use the Add/AddBase* functions to add named templates to the registry. Once all of the templates have been added, call Registry.Build(), which builds the function map and parses all of the templates.

You can render templates directly with Render, or render them within the context of a base template with RenderWithBase.

func NewRegistry

func NewRegistry() *Registry

NewRegistry returns a new, empty template registry.

func RegistryFromContext

func RegistryFromContext(ctx context.Context) *Registry

RegistryFromContext returns the registry from the ctx. It should have been added with Registry.Context(ctx).

func (*Registry) AddAllFS

func (r *Registry) AddAllFS(f fs.FS)

AddAllFS adds all paths in fs to the registry with paths as their name

func (*Registry) AddBaseFS

func (r *Registry) AddBaseFS(name, path string, fs fs.FS)

AddBaseFS adds a base template on path from the fs under name

func (*Registry) AddFS

func (r *Registry) AddFS(name, path string, fs fs.FS)

AddFS adds the named template from the given filesystem.

func (*Registry) AddPathFS

func (r *Registry) AddPathFS(path string, fs fs.FS)

AddPathFS adds path to the registry with the path as its name

func (*Registry) Build

func (r *Registry) Build() error

Build the registry templates. Caller should not add more templates after Build is called.

func (*Registry) Context

func (r *Registry) Context(ctx context.Context) context.Context

Context adds this registry to the context.

func (*Registry) Render

func (r *Registry) Render(w io.Writer, name string, ctx Ctx) error

Render the named template name to w using the provided context.

func (*Registry) RenderWithBase

func (r *Registry) RenderWithBase(w io.Writer, base, name string, ctx Ctx) error

RenderWithBase renders the template 'name' with the base template 'base' to the writer, using the provided context. The base template template will receive the child template's content as 'body', which it should incorporate into its structure.

type SproutReg

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

func (*SproutReg) LinkHandler

func (s *SproutReg) LinkHandler(hn sprout.Handler) error

func (*SproutReg) RegisterAliases

func (s *SproutReg) RegisterAliases(am sprout.FunctionAliasMap) error

func (*SproutReg) RegisterFunctions

func (s *SproutReg) RegisterFunctions(fm sprout.FunctionMap) error

func (*SproutReg) Uid

func (s *SproutReg) Uid() string

Jump to

Keyboard shortcuts

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