Documentation ¶
Overview ¶
package sprout provides template functions for Go.
This package contains a number of utility functions for working with data inside of Go `html/template` and `text/template` files.
To add these functions, use the `template.Funcs()` method:
t := templates.New("foo").Funcs(sprout.FuncMap())
Note that you should add the function map before you parse any template files.
In several cases, sprout reverses the order of arguments from the way they appear in the standard library. This is to make it easier to pipe arguments into functions.
See http://masterminds.github.io/sprout/ for more detailed documentation on each of the available functions.
Example ¶
// Set up variables and template. vars := map[string]interface{}{"Name": " John Jacob Jingleheimer Schmidt "} tpl := `Hello {{.Name | trim | lower}}` // Get the sprout function map. fmap := TxtFuncMap() t := template.Must(template.New("test").Funcs(fmap).Parse(tpl)) err := t.Execute(os.Stdout, vars) if err != nil { fmt.Printf("Error during template execution: %s", err) return }
Output: Hello john jacob jingleheimer schmidt
Index ¶
- func FuncMap(opts ...FunctionHandlerOption) template.FuncMap
- func GenericFuncMap() map[string]interface{}
- func HermeticHtmlFuncMap() template.FuncMap
- func HermeticTxtFuncMap() ttemplate.FuncMap
- func HtmlFuncMap() template.FuncMap
- func TxtFuncMap() ttemplate.FuncMap
- type DSAKeyFormat
- type ErrHandling
- type FunctionAliasMap
- type FunctionHandler
- type FunctionHandlerOption
- func WithAlias(originalFunction string, aliases ...string) FunctionHandlerOption
- func WithAliases(aliases FunctionAliasMap) FunctionHandlerOption
- func WithErrHandling(eh ErrHandling) FunctionHandlerOption
- func WithErrorChannel(ec chan error) FunctionHandlerOption
- func WithFunctionHandler(new *FunctionHandler) FunctionHandlerOption
- func WithLogger(l *slog.Logger) FunctionHandlerOption
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FuncMap ¶
func FuncMap(opts ...FunctionHandlerOption) template.FuncMap
FuncMap returns a template.FuncMap for use with text/template or html/template. It provides backward compatibility with sprig.FuncMap and integrates additional configured functions. FOR BACKWARD COMPATIBILITY ONLY
func GenericFuncMap ¶
func GenericFuncMap() map[string]interface{}
GenericFuncMap returns a copy of the basic function map as a map[string]interface{}.
func HermeticHtmlFuncMap ¶
HermeticHtmlFuncMap returns an 'html/template'.Funcmap with only repeatable functions.
func HermeticTxtFuncMap ¶
HermeticTxtFuncMap returns a 'text/template'.FuncMap with only repeatable functions.
func HtmlFuncMap ¶
HtmlFuncMap returns an 'html/template'.Funcmap
Types ¶
type DSAKeyFormat ¶
DSAKeyFormat stores the format for DSA keys. Used by pemBlockForKey
type ErrHandling ¶ added in v0.2.0
type ErrHandling int
ErrHandling defines the strategy for handling errors within FunctionHandler. It supports returning default values, panicking, or sending errors to a specified channel.
const ( // ErrHandlingReturnDefaultValue indicates that a default value should be // returned on error (default). ErrHandlingReturnDefaultValue ErrHandling = iota + 1 // ErrHandlingPanic indicates that a panic should be raised on error. ErrHandlingPanic // ErrHandlingErrorChannel indicates that errors should be sent to an error // channel. ErrHandlingErrorChannel )
type FunctionAliasMap ¶ added in v0.2.0
FunctionAliasMap is a map that stores a list of aliases for each function.
type FunctionHandler ¶ added in v0.2.0
type FunctionHandler struct { ErrHandling ErrHandling Logger *slog.Logger // contains filtered or unexported fields }
FunctionHandler manages function execution with configurable error handling and logging.
func NewFunctionHandler ¶ added in v0.2.0
func NewFunctionHandler(opts ...FunctionHandlerOption) *FunctionHandler
NewFunctionHandler creates a new FunctionHandler with the provided options.
func (*FunctionHandler) Hello ¶ added in v0.2.0
func (p *FunctionHandler) Hello() string
Hello returns a greeting string.
type FunctionHandlerOption ¶ added in v0.2.0
type FunctionHandlerOption func(*FunctionHandler)
FunctionHandlerOption defines a type for functional options that configure FunctionHandler.
func WithAlias ¶ added in v0.2.0
func WithAlias(originalFunction string, aliases ...string) FunctionHandlerOption
WithAlias returns a FunctionHandlerOption that associates one or more alias names with an original function name. This allows the function to be called by any of its aliases.
originalFunction specifies the original function name to which aliases will be added. aliases is a variadic parameter that takes one or more strings as aliases for the original function.
The function does nothing if no aliases are provided. If the original function name does not already have associated aliases in the FunctionHandler, a new slice of strings is created to hold its aliases. Each provided alias is then appended to this slice.
This option must be applied to a FunctionHandler using the FunctionHandler's options mechanism for the aliases to take effect.
Example:
handler := NewFunctionHandler(WithAlias("originalFunc", "alias1", "alias2"))
func WithAliases ¶ added in v0.2.0
func WithAliases(aliases FunctionAliasMap) FunctionHandlerOption
WithAliases returns a FunctionHandlerOption that configures multiple aliases for function names in a single call. It allows a batch of functions to be associated with their respective aliases, facilitating the creation of aliases for multiple functions at once.
This option must be applied to a FunctionHandler using the FunctionHandler's options mechanism for the aliases to take effect. It complements the WithAlias function by providing a means to configure multiple aliases in one operation, rather than one at a time.
Example:
handler := NewFunctionHandler(WithAliases(sprout.FunctionAliasMap{ "originalFunc1": {"alias1_1", "alias1_2"}, "originalFunc2": {"alias2_1", "alias2_2"}, }))
func WithErrHandling ¶ added in v0.2.0
func WithErrHandling(eh ErrHandling) FunctionHandlerOption
WithErrHandling sets the error handling strategy for a FunctionHandler.
func WithErrorChannel ¶ added in v0.2.0
func WithErrorChannel(ec chan error) FunctionHandlerOption
WithErrorChannel sets the error channel for a FunctionHandler.
func WithFunctionHandler ¶ added in v0.2.0
func WithFunctionHandler(new *FunctionHandler) FunctionHandlerOption
WithFunctionHandler updates a FunctionHandler with settings from another FunctionHandler. This is useful for copying configurations between handlers.
func WithLogger ¶ added in v0.2.0
func WithLogger(l *slog.Logger) FunctionHandlerOption
WithLogger sets the logger used by a FunctionHandler.