template

package
v0.5.9 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2020 License: MIT Imports: 11 Imported by: 0

README

postdog - Template plugin

This plugin provides template support for letters.

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(
				template.UseDir( // add template directories
					filepath.Join(wd, "testdata/templateDirs/dir1"),
					filepath.Join(wd, "testdata/templateDirs/dir2"),
				),
				template.Use("custom1", filepath.Join(wd, "testdata/templates/tpl1.html")), // add single template
				template.Use("custom2", filepath.Join(wd, "testdata/templates/tpl2.html")),
				template.UseFuncs(template.FuncMap{ // register template functions
					"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
}

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

Examples

Constants

This section is empty.

Variables

View Source
var Name = "template"

Name is the plugin name.

Functions

func AutowirePlugin added in v0.4.0

func AutowirePlugin(_ context.Context, cfg map[string]interface{}) (postdog.Plugin, error)

AutowirePlugin creates the templat plugin from the given cfg.

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 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 Register added in v0.4.0

func Register(cfg *autowire.Config)

Register registers the plugin factory in the autowire config.

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 []DirectoryConfig
	Funcs        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 DirectoryConfig added in v0.4.6

type DirectoryConfig struct {
	Dir     string
	Exclude func(string) bool
	Include func(string) bool
}

DirectoryConfig is the configuration for a template directory.

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(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.

func UseFuncs

func UseFuncs(funcMaps ...FuncMap) Option

UseFuncs adds the functions in funcMaps the templates' function maps.

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.

Directories

Path Synopsis
mock_i18n
Package mock_i18n is a generated GoMock package.
Package mock_i18n is a generated GoMock package.

Jump to

Keyboard shortcuts

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