render

package
v0.0.0-...-2262de7 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2023 License: NCSA Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoTemplateInitializer = errors.NewError("backend could find no suitable template initializer")

ErrNoTemplateInitializer is returned by backends that require the existence of a template configuration file, initalizer, or other support file at the top level directory of the requested template store.

Functions

func Loaders

func Loaders(loader ...pongo2.TemplateLoader) []pongo2.TemplateLoader

Loaders returns a slice of pongo2.TemplateLoaders for use in PongoOptions (below).

func MustZipLoader

func MustZipLoader(path string) pongo2.TemplateLoader

MustZipLoader creates a TemplateLoader from the ZIP archive located at the specified path. Similar to NewZipLoader, this function differs in that it will panic() if an error occ

func NewPongoVFSInheritanceLoader

func NewPongoVFSInheritanceLoader(basedir string, fs vfs.VFS) pongo2.TemplateLoader

func NewPongoVFSLoader

func NewPongoVFSLoader(opts *Options) pongo2.TemplateLoader

func NewZipLoader

func NewZipLoader(path string) (pongo2.TemplateLoader, error)

NewZipLoader creates a TemplateLoader from the ZIP archive located at the specified path. Before returning ZipLoader, this will first read *all* file names in the ZIP archive. For this reason, exceedingly large .zip files are not recommended.

Types

type BaseDirectoryReader

type BaseDirectoryReader struct {
	Path string
}

func (*BaseDirectoryReader) ListTemplates

func (b *BaseDirectoryReader) ListTemplates(path, glob string) ([]string, error)

func (*BaseDirectoryReader) ReadTemplate

func (b *BaseDirectoryReader) ReadTemplate(name string) (io.ReadCloser, error)

type BasicLogRenderer

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

func (*BasicLogRenderer) CompileDir

func (b *BasicLogRenderer) CompileDir(path string) error

func (*BasicLogRenderer) GlobalContext

func (b *BasicLogRenderer) GlobalContext(key string, value interface{})

func (*BasicLogRenderer) Render

func (b *BasicLogRenderer) Render(name string, value interface{}, w io.Writer) error

func (*BasicLogRenderer) SetFunc

func (b *BasicLogRenderer) SetFunc(name string, value interface{})

func (*BasicLogRenderer) SetLogger

func (b *BasicLogRenderer) SetLogger(logger *logging.Log)

type CachingReader

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

CachingReader is a TemplateReader that stores template data as it is read into local buffers. In most cases, this should not be used, and the template implementation's own internal caching should be used instead. This is provided for template engines that may or may not allow storing pre-processed or compiled templates.

func NewCachingReader

func NewCachingReader(reader TemplateReader) *CachingReader

type ContextRenderer

type ContextRenderer interface {
	// CloseNotifier closes any resources associated with the renderer. This is
	// especially important for renderers that use inotify internally to monitor
	// for changes.
	CloseNotifier() error

	// Context adds a new value to the context.
	Context(key string, value interface{})

	// ContextFunc adds a new function to the context.
	ContextFunc(key string, value interface{})

	// Render this context with the specified values.
	Render(name string, values interface{}, writer io.Writer) error
}

ContextRenderer is a limited-function renderer that encapsulates only context-specific rendering. It omits the global context, function setters, and compilation options.

type ExtendedLogRenderer

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

func (*ExtendedLogRenderer) CompileDir

func (e *ExtendedLogRenderer) CompileDir(path string) error

func (*ExtendedLogRenderer) GlobalContext

func (e *ExtendedLogRenderer) GlobalContext(key string, value interface{})

func (*ExtendedLogRenderer) Render

func (e *ExtendedLogRenderer) Render(name string, value interface{}, w io.Writer) error

func (*ExtendedLogRenderer) SetFunc

func (e *ExtendedLogRenderer) SetFunc(name string, value interface{})

func (*ExtendedLogRenderer) SetLogger

func (e *ExtendedLogRenderer) SetLogger(logger *logging.Log)

type FilesystemReader

type FilesystemReader struct {
	Path string
}

FilesystemReader is a TemplateReader that reads template data from the file system directly.

func (*FilesystemReader) ReadTemplate

func (fs *FilesystemReader) ReadTemplate(name string) io.ReadCloser

type InheritanceLoader

type InheritanceLoader struct{}

InheritanceLoader provides a backend loader that includes a base directory and a slice of directories with which to override the content contained in base. This is useful for distributing a default set of templates that may then be overridden individually by other styles.

Other loaders, including the ZipLoader, may be provided as well

type MapValues

type MapValues = pongo2.Context

type MultiRender

type MultiRender struct {
	Default   Renderer
	Templates map[string]Renderer
}

MultiRender is a template renderer that can be configured to render to multiple backends.

func NewMultiRenderer

func NewMultiRenderer(def Renderer) *MultiRender

func (*MultiRender) AddBackend

func (mt *MultiRender) AddBackend(name string, templater Renderer)

func (*MultiRender) Backend

func (mt *MultiRender) Backend(name string) Renderer

func (*MultiRender) CloseNotifier

func (mt *MultiRender) CloseNotifier() error

func (*MultiRender) CompileDir

func (mt *MultiRender) CompileDir(path string) error

func (*MultiRender) Render

func (mt *MultiRender) Render(name string, value interface{}, w io.Writer) error

func (*MultiRender) SetFunc

func (mt *MultiRender) SetFunc(name string, value interface{})

type MultiRenderer

type MultiRenderer interface {
	// AddBackend adds a renderer with the name `name` to this instance.
	AddBackend(name string, renderer Renderer)

	// Backend changes the renderer backend to one registered by the template
	// manager.
	Backend(name string) Renderer
}

MultiRenderer is used for proxying requests to mulitple Renderers. This may be useful for optionally logging template data for debugging purposes.

type NoopMultiRenderer

type NoopMultiRenderer struct {
	NoopRenderer
}

func (*NoopMultiRenderer) Backend

func (nmr *NoopMultiRenderer) Backend(name string) Renderer

func (*NoopMultiRenderer) CloseNotifier

func (nmr *NoopMultiRenderer) CloseNotifier() error

func (*NoopMultiRenderer) CompileDir

func (nmr *NoopMultiRenderer) CompileDir(path string) error

func (*NoopMultiRenderer) ContextRenderer

func (nmr *NoopMultiRenderer) ContextRenderer() ContextRenderer

func (*NoopMultiRenderer) GlobalContext

func (nmr *NoopMultiRenderer) GlobalContext(name string, value interface{})

func (*NoopMultiRenderer) Render

func (nmr *NoopMultiRenderer) Render(name string, values interface{}, writer io.Writer) error

func (*NoopMultiRenderer) SetFunc

func (nmr *NoopMultiRenderer) SetFunc(name string, value interface{})

func (*NoopMultiRenderer) SetLogger

func (nmr *NoopMultiRenderer) SetLogger(logger *logging.Log)

type NoopRenderer

type NoopRenderer struct{}

func (*NoopRenderer) CompileDir

func (nr *NoopRenderer) CompileDir(path string) error

func (*NoopRenderer) GlobalContext

func (nr *NoopRenderer) GlobalContext(name string, value interface{})

func (*NoopRenderer) Render

func (nr *NoopRenderer) Render(name string, values interface{}, w io.Writer) error

func (*NoopRenderer) SetFunc

func (nr *NoopRenderer) SetFunc(name string, values interface{})

type NotifyLoader

type NotifyLoader interface {
	Watch(func())
	CloseWatches() error
}

type Options

type Options struct {
	// MonitorChanges to the template directory and clear any internal caches
	// when a source file is modified, added, or removed.
	MonitorChanges bool

	// TemplateRoot to use for the base path. Any files above this path will be
	// inaccessible to the renderer.
	TemplateRoot string

	// Path to theme. Themes are isolated template directories that do not
	// interact with each other but share a common TemplateRoot.
	ThemePath string

	// VFS, if set, will configure the target backend with the specified virtual
	// file system.
	VFS vfs.VFS
}

type Pongo2ContextRenderer

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

func (*Pongo2ContextRenderer) CloseNotifier

func (c *Pongo2ContextRenderer) CloseNotifier() error

func (*Pongo2ContextRenderer) Context

func (c *Pongo2ContextRenderer) Context(key string, value interface{})

func (*Pongo2ContextRenderer) ContextFunc

func (c *Pongo2ContextRenderer) ContextFunc(key string, value interface{})

func (*Pongo2ContextRenderer) Render

func (c *Pongo2ContextRenderer) Render(name string, values interface{}, w io.Writer) error

type Pongo2Renderer

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

Pongo2Renderer defines a renderer that implements Renderer using Pongo2 as a backend.

func MustPongo2Renderer

func MustPongo2Renderer(setname, basedir string) *Pongo2Renderer

MustPongo2Renderer creates a new Pongo2 renderer instance or panics if an error occurs.

func MustPongo2RendererWithOptions

func MustPongo2RendererWithOptions(setname, basedir string, options *PongoOptions) *Pongo2Renderer

MustPongo2RendererWithOptions creates a new Pongo2 instance configured with the defeind `options` (see PongoOptions). If an error occurs during initialization, this will panic.

func NewPongo2Renderer

func NewPongo2Renderer(setname, basedir string) (*Pongo2Renderer, error)

NewPongo2Renderer creates a new Pongo2Renderer instance using `setname` for the TemplateSet name and `basedir` as the template's base directory.

For more information on TemplateSets, please refer to the Pongo2 documentation. We use TemplateSets for isolating template context as multiple Renderer instances can be used within Capstan, either globally or per route/controller:

https://godoc.org/github.com/flosch/pongo2#TemplateSet

func NewPongo2RendererWithOptions

func NewPongo2RendererWithOptions(setname, basedir string, options *PongoOptions) (*Pongo2Renderer, error)

NewPongo2RendererWithoptions creates a new Pongo2Renderer instance configured with the defined `options` (see PongoOptions).

func Pongo2WithLoader

func Pongo2WithLoader(setname string, loader pongo2.TemplateLoader) *Pongo2Renderer

func (*Pongo2Renderer) AddLoader

func (p *Pongo2Renderer) AddLoader(loader pongo2.TemplateLoader) *Pongo2Renderer

AddLoader to the underlying Pongo2 instance.

func (*Pongo2Renderer) CloseNotifier

func (p *Pongo2Renderer) CloseNotifier() error

Close the renderer and any fsnotify instances.

func (*Pongo2Renderer) CompileDir

func (p *Pongo2Renderer) CompileDir(path string) error

CompileDir is not implemented (or required) for Pongo2.

func (*Pongo2Renderer) ContextRenderer

func (p *Pongo2Renderer) ContextRenderer() ContextRenderer

func (*Pongo2Renderer) GlobalContext

func (p *Pongo2Renderer) GlobalContext(name string, value interface{})

GlobalContext sets a value in the template's global context. For Pongo2, this will set a value for the context used by the TemplateSet backing this renderer.

func (*Pongo2Renderer) Purge

func (p *Pongo2Renderer) Purge()

Purge Pongo2 cache.

func (*Pongo2Renderer) Render

func (p *Pongo2Renderer) Render(name string, values interface{}, w io.Writer) error

Render the template `name` using `values` whilst writing to `w`. Returns an error if the template render fails.

func (*Pongo2Renderer) SetFunc

func (p *Pongo2Renderer) SetFunc(name string, value interface{})

SetFunc sets a function of `name` within the global context. This will not overwrite values that already exist.

This is a Pongo2Renderer.GlobalContext() wrapper.

func (*Pongo2Renderer) SetLogger

func (p *Pongo2Renderer) SetLogger(logger *logging.Log)

SetLogger to the specified logger instance.

type PongoOptions

type PongoOptions struct {
	ProhibitedFilters []string
	ProhibitedTags    []string
	TrimBlocks        bool
	LStripBlocks      bool
	Debug             bool
	GlobalContext     pongo2.Context
	Loaders           []pongo2.TemplateLoader
	Logger            *logging.Log

	// DisableReload when templates change. Uses fsnotify to detect changes.
	// This should be set to true in production mode.
	DisableReload bool
}

PongoOptions provides a configuration object for Pongo2's initialization.

type PongoVFSInheritanceLoader

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

func (*PongoVFSInheritanceLoader) Abs

func (l *PongoVFSInheritanceLoader) Abs(base, name string) string

func (*PongoVFSInheritanceLoader) Get

func (*PongoVFSInheritanceLoader) Watch

func (l *PongoVFSInheritanceLoader) Watch(callback func())

type PongoVFSLoader

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

func (*PongoVFSLoader) Abs

func (l *PongoVFSLoader) Abs(base, name string) string

func (*PongoVFSLoader) CloseWatches

func (l *PongoVFSLoader) CloseWatches() error

func (*PongoVFSLoader) Get

func (l *PongoVFSLoader) Get(path string) (io.Reader, error)

func (*PongoVFSLoader) Watch

func (l *PongoVFSLoader) Watch(callback func())

type Renderer

type Renderer interface {
	// CloseNotifier closes any inotify resources associated with the Renderer.
	// If the Renderer doesn't implement inotify, this should be a noop. Some
	// Capstan templates may use fsnotifier internally, and this should be
	// called before re-exec'ing the process, e.g. via routing.Relaunch().
	CloseNotifier() error

	// CompileDir compiles all templates in the specified path. Not all Renderer
	// implementations may support this; those that don't will implement this as
	// a noop.
	CompileDir(path string) error

	// GlobalContext adds a value to the global context for a given template.
	// Not all backends support context assignment.
	GlobalContext(key string, value interface{})

	// Render the template specified by name using the data `values` as its
	// source. Depending on the backend, `name` may reference a file name or any
	// other symbolic name the Renderer recognizes as valid. The content of
	// `values` may be a struct or map, depending on the backend; for Go
	// templates, any struct value can be provided. For pongo2 and others, this
	// must be a map.
	//
	// Implementations that don't natively return errors will attempt to map the
	// underlying template library's behavior to a sensible error. Errors may be
	// wrapped.
	Render(name string, values interface{}, writer io.Writer) error

	// ContextRenderer returns a Renderer that may be used to configure
	// additional context, such as request context(s).
	ContextRenderer() ContextRenderer

	// SetFunc is used to set the value of a function for use within the
	// template. Typically used for setting global functions like URLFor, this
	// provides a proxy for the route constructor to assign Capstan-specific
	// calls.
	//
	// Some backends may implement this as a wrapper around GlobalContext.
	SetFunc(name string, value interface{})

	// SetLogger to the specified logger instance. If unset, the renderer will
	// create its own root logger.
	SetLogger(*logging.Log)
}

Renderer defines the interface required by backends that intend to be used for rendering templates used by Capstan. Individual Renderers may be used either globally or per controller.

By using unique Renderers in this manner, it is possible to define different template backends depending on the route requested by the client.

func New

func New(backend, name string, options *Options) (Renderer, error)

type TemplateReader

type TemplateReader interface {
	// ListTemplates in a give `path` matching the pattern `glob`.
	ListTemplates(path, glob string) ([]string, error)

	// ReadTemplate accepts a name argument and returns a ReadCloser that
	// produces template data.
	ReadTemplate(name string) (io.ReadCloser, error)
}

TemplateReader defines an interface used by Renderer implementations that do not provide a reader mechanism on their own.

Currently unused.

type TextRenderer

type TextRenderer interface {
	// RenderStream operates similarly to Render() with the exception that it
	// forgoes rendering sources from the file system and instead renders a
	// template passed in via a readable stream.
	RenderStream(reader io.Reader, values interface{}, writer io.Writer) error

	// RenderText operates similarly to Render() with the exception that it
	// forgoes rendering sources from the file system and instead renders a
	// template passed in via a string.
	RenderText(template string, values interface{}, writer io.Writer) error
}

TextRenderer is a renderer that provides plain-text rendering. This type is specified separately from Renderer and ContextRenderer because not all backends are required to support rendering directly from textual input.

type ZipLoader

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

ZipLoader is a pongo2.TemplateLoader implementation that loads template data from a .zip archive.

This implementation should not be used for especially large .zip files as paths are mapped to a Golang map with blatant disregard for any particular pattern. Future implementations may provide a means of filtering template data from the ZIP.

ZipLoader is useful for distributing stock templates, etc., within a single ZIP archive for application first-start or similar.

func (*ZipLoader) Abs

func (z *ZipLoader) Abs(base, name string) string

Abs returns the absolute path of the named file using `base` as derived from Pongo's template inheritance.

This may need further testing.

func (*ZipLoader) Get

func (z *ZipLoader) Get(path string) (io.Reader, error)

Get returns a reader for the template named by `path`, loading it from the cached file names as read from the .zip archive.

Jump to

Keyboard shortcuts

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