Categorygithub.com/go-sprout/sprout
modulepackage
0.6.0
Repository: https://github.com/go-sprout/sprout.git
Documentation: pkg.go.dev

# README

Sprout Logo

[!NOTE] Sprout is an evolved variant of the Masterminds/sprig library, reimagined for modern Go versions. It introduces fresh functionalities and commits to maintaining the library, picking up where Sprig left off. Notably, Sprig had not seen updates for two years and was not compatible beyond Golang 1.13, necessitating the creation of Sprout.

Motivation

Sprout was born out of the need for a modernized, maintained, and performant template function library. Sprig, the predecessor to Sprout, had not seen updates for two years and was not optimized for later versions of Golang. Sprout aims to fill this gap by providing a library that is actively maintained, compatible with the latest Go versions, and optimized for performance.

Roadmap to Sprout v1.0

You can track our progress towards Sprout v1.0 by following the documentation page here.

Table of Contents

Transitioning from Sprig

Sprout provide a package sprigin to provide a drop-in replacement for Sprig in the v1.0, with the same function names and behavior. To use Sprout in your project, simply replace the Sprig import with sprigin:

[!IMPORTANT] The sprigin package is a temporary solution to provide backward compatibility with Sprig. We recommend updating your code to use the Sprout package directly to take advantage of the new features and improvements.

A complete guide are available in the documentation.

import (
-  "github.com/Masterminds/sprig/v3"
+  "github.com/go-sprout/sprout/sprigin"
)

tpl := template.Must(
  template.New("base").
-   Funcs(sprig.FuncMap()).
+   Funcs(sprigin.FuncMap()).
    ParseGlob("*.tmpl")
)

Usage

Creating a Handler

A handler in Sprout is responsible for managing the function registries and functions. The DefaultHandler is the primary implementation provided by Sprout.

import "github.com/go-sprout/sprout"

handler := sprout.New()

Customizing the Handler

Sprout supports various customization options using handler options:

handler := sprout.New(
  // Add your logger to the handler to log errors and debug information using the
  // standard slog package or any other logger that implements the slog.Logger interface.
  // By default, Sprout uses a slog.TextHandler.
  sprout.WithLogger(slogLogger),
  // Set the alias for a function. By default, Sprout use alias for some functions for backward compatibility with Sprig.
  sprout.WithAlias("hello", "hi"),
)

Working with Registries

Registries in Sprout are groups of functions that can be added to a handler. They help organize functions and optimize template performance.

You can retrieve all built-ins registries and functions under Registries.

import (
  "github.com/go-sprout/sprout/registry/conversion" // toString, toInt, toBool, ...
  "github.com/go-sprout/sprout/registry/std" // default, empty, any, all, ...
)

//...

handler.AddRegistries(
  conversion.NewRegistry(),
  std.NewRegistry(),
)

Building Function Maps

To use Sprout with templating engines like html/template or text/template, you need to build the function map:

funcs := handler.Build()
tpl := template.New("example").Funcs(funcs).Parse(`{{ hello }}`)

Working with Templates

Once your function map is ready, you can use it to render templates:

tpl, err := template.New("example").Funcs(funcs).Parse(`{{ myFunc }}`)
if err != nil {
    log.Fatal(err)
}
tpl.Execute(os.Stdout, nil)

This will render the template with all functions and aliases available.

Usage: Quick Example

Here is a quick example of how to use Sprout with the text/template package:

package main

import (
	"os"
	"text/template"

	"github.com/go-sprout/sprout"
	"github.com/go-sprout/sprout/registry/std"
)

func main() {
	handler := sprout.New()
	handler.AddRegistry(std.NewRegistry())

	tpl := template.Must(
    template.New("example").Funcs(handler.Build()).Parse(`{{ hello }}`),
  )
	tpl.Execute(os.Stdout, nil)
}

Performence Benchmarks

To see all the benchmarks, please refer to the benchmarks directory.

Sprig v3.2.3 vs Sprout v0.5

goos: linux
goarch: amd64
pkg: sprout_benchmarks
cpu: Intel(R) Core(TM) i9-9900K CPU @ 3.60GHz
BenchmarkSprig-16              1        2991811373 ns/op        50522680 B/op      32649 allocs/op
BenchmarkSprout-16             1        1638797544 ns/op        42171152 B/op      18061 allocs/op
PASS
ok      sprout_benchmarks       4.921s

Time improvement: ((2991811373 - 1638797544) / 2991811373) * 100 = 45.3% Memory improvement: ((50522680 - 42171152) / 50522680) * 100 = 16.5%

So, Sprout v0.5 is approximately 45.3% faster and uses 16.5% less memory than Sprig v3.2.3. 🚀

You can see the full benchmark results here.

Development Philosophy (Currently in reflexion to create our)

Our approach to extending and refining Sprout was guided by several key principles:

  • Build on the principles of simplicity, flexibility, and consistency.
  • Empower developers to create robust templates without sacrificing performance or usability.
  • Adheres strictly to Go's templating conventions, ensuring a seamless experience for those familiar with Go's native tools.
  • Naming conventions across functions are standardized for predictability and ease of use.
  • Emphasizes error handling, preferring safe defaults over panics.
  • Provide a clear and comprehensive documentation to help users understand the library and its features.
  • Maintain a high level of code quality, ensuring that the library is well-tested, performant, and reliable.
  • Continuously improve and optimize the library to meet the needs of the community.
  • Avoids any external dependencies within template functions, ensuring all operations are self-contained and reliable.
  • Performance is a key consideration, with a focus on optimizing the library for speed and efficiency.

# Packages

This package are used to store all helpers functions used to validate and log deprecated functions and signatures in the project codebase.
* * Pesticide are a package to help you to test your functions on a template * engine.
No description provided by the author
No description provided by the author

# Functions

AddAlias adds an alias for the original function name.
AddFunction adds a new function under the specified name to the given registry.
AddNotice adds a new function notice to the given function.
AssignAliases assigns all aliases defined in the handler to their original functions.
AssignNotices assigns all notices defined in the handler to their original functions.
AssignSafeFuncs wraps all functions with a safe wrapper that logs any errors that occur during function execution.
ErrRecoverPanic are an utility function to recover panic from a function and set the error message to unsure no panic is thrown in the template engine.
New creates and returns a new instance of DefaultHandler with optional configurations.
NewDebugNotice creates a new debug function notice with the given function name and message.
NewDeprecatedNotice creates a new deprecated function notice with the given function name and message.
NewErrConvertFailed is an utility function to create a new error message when converting a value to another type fails.
NewInfoNotice creates a new informational function notice with the given function name and message.
NewNotice creates a new function notice with the given function names, kind, and message.
WithAlias returns a FunctionOption[**DefaultHandler] that associates one or more alias names with an original function name.
WithAliases returns a FunctionOption[**DefaultHandler] that configures multiple aliases for function names in a single call.
WithHandler updates a DefaultHandler with settings from another DefaultHandler.
WithLogger sets the logger used by a DefaultHandler.
WithNotices is used to add one or more function notices to the handler.
WithRegistries returns a HandlerOption that adds the provided registries to the handler.
WithSafeFuncs enables safe function calls in a DefaultHandler.

# Constants

NoticeKindDebug indicates that the notice is for debugging purposes.
NoticeKindDeprecated indicates that the function is deprecated.
NoticeKindInfo indicates that the notice is informational.

# Variables

ErrConvertFailed is an error message when converting a value to another type fails.

# Structs

DefaultHandler manages function execution with configurable error handling and logging.
No description provided by the author

# Interfaces

Handler is the interface that wraps the basic methods of a handler to manage all registries and functions.
Registry is an interface that defines the method to register functions within a given Handler.
No description provided by the author
No description provided by the author

# Type aliases

FunctionAliasMap is a map that stores a list of aliases for each function.
FunctionMap is an alias for template.FuncMap, which maps function names to functions.
HandlerOption[Handler] defines a type for functional options that configure a typed Handler.
NoticeKind represents the type of notice that can be applied to a function.