template

package
v0.0.0-...-4998ed7 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2021 License: MIT Imports: 11 Imported by: 6

Documentation

Overview

Package template provides a template renderer.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Render

func Render(r io.Reader, w io.Writer, data map[string]interface{}, s *scope.Scope, evaluatorOpts ...evaluator.Opt) error

Render loads a template from r, evaluates it using scope s, optionally passing additional data, and writes the output to w.

Types

type Loader

type Loader interface {
	Load(name string) (io.ReadCloser, error)
}

A Loader loads a template with a specific name and returns it as a reader.

type LoaderFunc

type LoaderFunc func(name string) (io.ReadCloser, error)

A LoaderFunc is an adapter type that allows ordinary functions to be used as Loaders. If f is a function with the appropriate signature, LoaderFunc(f) is a loader that calls f.

func (LoaderFunc) Load

func (l LoaderFunc) Load(name string) (io.ReadCloser, error)

type Opt

type Opt func(*Renderer)

Opt is the type of a function that configures r.

func WithScopeData

func WithScopeData(k string, v interface{}) Opt

WithScopeData configures a renderer to provide additional data to all templates being rendered. WithScopeData may be used multiple times, also in combination with WithScopeDataMap.

func WithScopeDataMap

func WithScopeDataMap(data map[string]interface{}) Opt

WithScopeDataMap configures a renderer to provide additional data to all templates being rendered. WithScopeDataMap may be used multiple times, also in combination with WithScopeData.

func WithTemplateFuncName

func WithTemplateFuncName(n string) Opt

WithTemplateFuncName configures a renderer to use n as the name of the function that may be called in templates to render other templates. The default name of this function is "t".

The signature of that function is as follows:

func(name string, data map[string]interface{}) SafeString

where name is the name of the template to render.

The data map is in turn provided to the new renderer using WithScopeData.

type Renderer

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

Renderer parses templates, evaluates their code, and writes out the output.

func NewRenderer

func NewRenderer(loader Loader, opts ...Opt) *Renderer

NewRenderer returns a new renderer, configured with opts, that loads templates via load.

func (*Renderer) Render

func (r *Renderer) Render(ctx context.Context, w io.Writer, name string, data map[string]interface{}) error

Render loads a template with a specific name, evaluates it (optionally passing additional data), and writes the output to w.

If the template calls the renderer's function to render other templates (see WithTemplateFuncName), the data map passed to Render will not be passed to those templates.

Literal output is wrapped in SafeString without further escaping.

The context is passed to an internal evaluator.ArgumentResolver and can therefore be resolved automatically as an argument to method or function calls in template code.

type SafeString

type SafeString string

SafeString encapsulates a regular string to mark it as safe for output. If template code tries to output a regular string, it will be rendered only as "!UNSAFE!". Instead, regular strings must be wrapped in SafeString to render them as expected. Before wrapping in SafeString, strings should be HTML-escaped etc., depending on the output's language.

func (SafeString) String

func (s SafeString) String() string

Jump to

Keyboard shortcuts

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