template

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2020 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package template provides template support for letter bodies.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Data

func Data(ctx context.Context) interface{}

Data returns the data that is attached to ctx.

func Disable

func Disable(ctx context.Context) context.Context

Disable disables templates for this context.

func Enable

func Enable(ctx context.Context, name string, data interface{}) context.Context

Enable sets the template that will be used to build the letter body for this context. Use the optional data that can be accesed in the template via {{ .Data }}

func For

func For(ctx context.Context) (string, interface{}, bool)

For returns the template and data that should be used for sending letters with ctx. Returns false if ctx has no template set.

func Name

func Name(ctx context.Context) (string, bool)

Name returns the template that should be used for sending letters with ctx. Returns false if ctx has no template set.

func Plugin

func Plugin(opts ...Option) postdog.PluginFunc

Plugin creates the template plugin. It panics if it fails to parse the templates. Use TryPlugin() if you need to catch parse errors.

Example:

plugin := template.Plugin(
	template.UseDir("/templates")
)

func TryPlugin

func TryPlugin(opts ...Option) (postdog.PluginFunc, error)

TryPlugin creates the template plugin. It doesn't panic when it fails to parse the templates.

Types

type Config

type Config struct {
	Templates    map[string]string
	TemplateDirs []string
	Funcs        template.FuncMap
}

Config is the plugin configuration.

func (Config) ParseTemplates

func (cfg Config) ParseTemplates() (*template.Template, error)

ParseTemplates parses the templates that are configured in cfg and returns the root template.

type FuncMap

type FuncMap template.FuncMap

FuncMap is an alias to template.FuncMap.

type Option

type Option func(*Config)

Option is a plugin option.

func Use

func Use(name, filepath string) Option

Use registers the template at filepath under name.

func UseDir

func UseDir(dirs ...string) Option

UseDir registers all files in dirs and their subdirectories as templates. The template name will be set to the relative path of the file to the given directory in dirs, where every directory separator is replaced by a dot and the file extensions is removed.

Example:

Given the following files:
/templates/tpl1.html
/templates/tpl2.html
/templates/nested/tpl3.html
/templates/nested/deeper/tpl4.html

UseDir("/templates") will result in the following template names:
- tpl1
- tpl2
- nested.tpl3
- nested.deeper.tpl4

func UseFuncs

func UseFuncs(funcMaps ...FuncMap) Option

UseFuncs ...

Jump to

Keyboard shortcuts

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