Documentation ¶
Overview ¶
Package template provides a template renderer.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 ¶
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 ¶
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 ¶
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 ¶
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