extensions

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 20, 2023 License: GPL-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func View

func View(options *Options, ext Extension) func(w http.ResponseWriter, r *http.Request)

View returns a view that renders the extension into the base template.

Types

type Base

type Base struct {
	// The name of the extension.
	// This is used to uniquely identify the extension.
	ExtensionName string

	// The callback that is called when the extension is rendered.
	// This is used to get the template data and the template name.
	// The template name is the name of the template to render.
	// This is not the file name, but the name of the template.
	// IE: {{template "base" .}}
	Callback func(w http.ResponseWriter, r *http.Request) map[string]any
}

Base extension This is the base extension.

type Block

type Block struct {
	// The name of the block.
	// IE: {{define "content"}}, the block name is "content".
	BlockName string
	// The value of the block.
	// IE: {{define "content"}}Hello World{{end}}, the value is "Hello World".
	Value string
}

Block is a block of text that is rendered before or after the template.

type Extension

type Extension interface {
	// The name of the extension.
	// This is used to uniquely identify the extension.
	Name() string

	// Extra data for the extension when it is rendered.
	View(w http.ResponseWriter, r *http.Request) map[string]any
}

Template extensions These are extensions that are rendered into the base template. This is useful, if you want to allow people from other packages to extend base templates of for example; an admin panel.

type ExtensionWithFilename

type ExtensionWithFilename interface {
	Extension
	// The file name for the extension.
	// This is the name of the template to render.
	// The template will be fetched from a template.Manager.
	Filename() string
}

ExtensionWithFilename is an extension that exists of a file. This is an addition to the default extensions, where you can specify your own file as a template.

type ExtensionWithStrings

type ExtensionWithStrings interface {
	Extension
	String(w http.ResponseWriter, r *http.Request) string
}

ExtensionWithStrings is an extension that exists of a string. This is an addition to the default extensions, where you can specify your own string as a template.

type ExtensionWithTemplate

type ExtensionWithTemplate interface {
	Extension
	Template(w http.ResponseWriter, r *http.Request) *template.Template
}

ExtensionWithTemplate is an extension that has a template. This is an addition to the default extensions, where you can specify your own template.

type Options

type Options struct {
	// BaseManager is the manager for the base template.
	// It is used to get the base template, and to parse the extension template.
	BaseManager *templates.Manager

	// ExtensionManager is the manager for the extension template.
	ExtensionManager *templates.Manager

	// TemplateName is the name of the template to use as the base.
	// This is not the file name, but the name of the template.
	// IE: {{template "base" .}}
	TemplateName string

	// BlockName is the name of the block to render the template into.
	// IE: {{define "content"}}
	BlockName string

	// Called when an error occurs.
	OnError func(w http.ResponseWriter, r *http.Request, err error)

	// Called before the template is rendered.
	BeforeRender func(w http.ResponseWriter, r *http.Request, t *template.Template)

	// Custom blocks
	// These are blocks that are rendered before and after the template.
	CSS *Block
	JS  *Block
}

Default options for the extension view. This is used to render the extension into the base template.

type WithFilename

type WithFilename struct {
	Base
	// The file name for the extension.
	// This is the name of the template to render.
	FileName string
}
extension struct.

This is used to render the extension into the base template. Avoids having to create a new struct for each extension.

func (*WithFilename) Filename

func (s *WithFilename) Filename() string

Returns the file name of the extension. This is the name of the template to render. The template will be fetched from a template.Manager.

func (*WithFilename) Name

func (s *WithFilename) Name() string

Returns the name of the extension.

func (*WithFilename) View

func (s *WithFilename) View(w http.ResponseWriter, r *http.Request) map[string]any

Returns the template data for the extension.

type WithStrings

type WithStrings struct {
	Base
	HTMLString string
}

func (*WithStrings) Name

func (s *WithStrings) Name() string

Returns the name of the extension.

func (*WithStrings) String

func (s *WithStrings) String(w http.ResponseWriter, r *http.Request) string

func (*WithStrings) View

func (s *WithStrings) View(w http.ResponseWriter, r *http.Request) map[string]any

Returns the template data for the extension.

type WithTemplate

type WithTemplate struct {
	Base
	HTMLTemplate *template.Template
}

func (*WithTemplate) Name

func (s *WithTemplate) Name() string

Returns the name of the extension.

func (*WithTemplate) Template

func (*WithTemplate) View

func (s *WithTemplate) View(w http.ResponseWriter, r *http.Request) map[string]any

Returns the template data for the extension.

Jump to

Keyboard shortcuts

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