Getting Started
A quick start guide to understand and use Sprout in your project
Last updated
A quick start guide to understand and use Sprout in your project
Last updated
This page evolves with each version based on modifications and community feedback. Having trouble following this guide?
Open an issue to get help, and contribute to improving this guide for future users.
Sprout is a powerful and flexible templating engine designed to help you manage and organize template functions efficiently. It allows developers to register and manage functions across different registries, offering features such as aliasing, error handling, and logging.
To get started with Sprout, first install the package:
A handler in Sprout is responsible for managing the function registries and functions. The DefaultHandler
is the primary implementation provided by Sprout.
Sprout supports various customization options using handler options:
Logger Configuration: You can customize the logging behavior by providing a custom logger:
Load Registry:
You can load a registry directly on your handler using the WithRegistries
option:
See more below or in dedicated page Loader System (Registry).
Load Registry Group:
You can load a group of registries directly on your handler using the WithGroups
option:
See more below or in dedicated page Loader System (Registry Group).
Aliases Management: You can specify your custom aliases directly on your handler:
See more below or in dedicated page Function Aliases.
Notices: You can specify a notice (info, deprecated, warn) on specific function, this message has written on the logger at runtime when rendering the template:
See more below or in dedicated page Function Notices.
Safe functions: Sprout usually returns an error if something goes wrong. To continue rendering and only log the error, enable the Safe Function feature:
See more below or in dedicated page Safe Functions.
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 List of all registries.
To create your own, see the dedicated page How to create a registry.
Once your registry is implemented, you can add it to a handler:
You can also add multiple registries at once:
Or add registries directly when initializing the handler:
Sprout supports function aliases, allowing you to call the same function by different names.
You can add aliases for functions in your handler configuration:
Aliases are automatically resolved when you build your handler’s function map.
To use Sprout with templating engines like html/template
or text/template
, you need to build the function map:
This prepares all registered functions and aliases for use in templates. This also caches the function map for better performance.
Once your function map is ready, you can use it to render templates:
This will render the template with all functions and aliases available.
Sprout provides a structured and powerful way to manage template functions in Go, making it easier to build, maintain, and extend templating functionality. With features like custom registries, aliases, and configurable error handling, Sprout can significantly enhance your templating experience.
For more information or questions, refer to the Sprout GitHub repository.