Documentation ¶
Overview ¶
Package template provides template support for letter bodies.
Example ¶
package main import ( "context" "os" "path/filepath" "strings" "time" "github.com/bounoable/postdog" "github.com/bounoable/postdog/letter" "github.com/bounoable/postdog/plugin/template" "github.com/bounoable/postdog/plugin/template/i18n" ) func main() { wd, _ := os.Getwd() po := postdog.New( postdog.WithPlugin( template.Plugin( // add template directories template.UseDir(filepath.Join(wd, "testdata/templateDirs/dir1")), template.UseDir(filepath.Join(wd, "testdata/templateDirs/dir2")), // add single templates template.Use("custom1", filepath.Join(wd, "testdata/templates/tpl1.html")), template.Use("custom2", filepath.Join(wd, "testdata/templates/tpl2.html")), // add template funcs template.UseFuncs(template.FuncMap{ "title": strings.Title, "upper": strings.ToUpper, }), i18n.Use(nil /* `i18n.Translator` implementation */), // register `$t` (translate) template function ), ), ) // Enable plugin for this context, use the template "dir2.nested.tpl7" and make custom data available in the template. ctx := template.Enable(context.Background(), "dir2.nested.tpl7", map[string]interface{}{ "Name": "bob", "Now": time.Now(), }) err := po.Send(ctx, letter.Write( letter.HTML(`Hello {{ title .Data.Name }}, today is {{ .Data.Now.Format "2006/01/02" }}.`), )) _ = err }
Output:
Index ¶
- Variables
- func AutowirePlugin(_ context.Context, cfg map[string]interface{}) (postdog.Plugin, error)
- func Disable(ctx context.Context) context.Context
- func Enable(ctx context.Context, name string, data interface{}) context.Context
- func For(ctx context.Context) (string, interface{}, bool)
- func Plugin(opts ...Option) postdog.PluginFunc
- func Register(cfg *autowire.Config)
- func TryPlugin(opts ...Option) (postdog.PluginFunc, error)
- type Config
- type DirectoryConfig
- type FuncMap
- type Option
- type UseDirOption
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var Name = "template"
Name is the plugin name.
Functions ¶
func AutowirePlugin ¶ added in v0.4.0
AutowirePlugin creates the templat plugin from the given cfg.
func Enable ¶
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 ¶
For returns the template and data 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") )
Types ¶
type Config ¶
type Config struct { Templates map[string]string TemplateDirs []DirectoryConfig Funcs FuncMap }
Config is the plugin configuration.
type DirectoryConfig ¶ added in v0.4.6
DirectoryConfig is the configuration for a template directory.
type Option ¶
type Option func(*Config)
Option is a plugin option.
func UseDir ¶
func UseDir(dir string, opts ...UseDirOption) 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
Use the `Exclude()` option to filter templates based on their filepath.
type UseDirOption ¶ added in v0.4.6
type UseDirOption func(*DirectoryConfig)
UseDirOption is an option for the `UseDir()` function.
func Exclude ¶ added in v0.4.6
func Exclude(fn func(string) bool) UseDirOption
Exclude filters templates by their filepath.
func ExcludePattern ¶ added in v0.4.6
func ExcludePattern(pattern string) UseDirOption
ExcludePattern filters templates by a pattern. This function panics if it can't compile the pattern.
func ExcludeRegex ¶ added in v0.4.6
func ExcludeRegex(expr *regexp.Regexp) UseDirOption
ExcludeRegex filters templates by a regular expression.
func Include ¶ added in v0.5.7
func Include(fn func(string) bool) UseDirOption
Include filters templates by their filepath.
func IncludePattern ¶ added in v0.5.7
func IncludePattern(pattern string) UseDirOption
IncludePattern filters templates by a pattern. This function panics if it can't compile the pattern.
func IncludeRegex ¶ added in v0.5.7
func IncludeRegex(expr *regexp.Regexp) UseDirOption
IncludeRegex filters templates by a regular expression.