Loader System (Registry)

Need just 10 functions or want to create your own? Introducing: the Registry

This feature was developed based on a Request for Comments (RFC) discussed within the Sprout project community. The RFC outlines the need for a modular registry system to improve the flexibility and maintainability of the codebase. For more detailed information on the discussion and rationale behind this implementation, you can review the full RFC here.

Introduction

Sprout empowers you to create and manage your own registry of functions—a versatile collection that you can store, share, and seamlessly integrate into your projects. Additionally, you can leverage functions contributed by other users, enhancing collaboration and expanding the functionality of your applications.

Goals

  • Optimize Build Time: Use only the registry functions you need to reduce both build time and binary size.

  • Ease of Use: Import a registry with just one line of code for quick integration.

  • Reusability: Reuse functions across multiple projects without redundancy.

  • Collaboration: Incorporate registries created by others to enhance your projects.

  • Sharing: Share your registry with others or keep it private.

  • Versioning: Manage specific versions of a registry as a Go package to ensure consistency across projects.

How to use a registry

To use a registry in a project, follow these steps:

// Initialize your sprout handler
handler := sprout.New()

// Add one registry
handler.AddRegistry(ownregistry.NewRegistry())
// Add more than one at the same time
handler.AddRegistries(
  ownregistry.NewRegistry(),
  // ...
)

// Build your FunctionMap based on your handler
tpl := template.Must(
    template.New("base").Funcs(handler.Build()).ParseGlob("*.tmpl"),
  )

You can also use the option to add registries when initializing the handler:

handler := sprout.New(
  sprout.WithRegistries(reg1.NewRegistry(), reg2.NewRegistry()),
)

This code sets up your project to utilize the functions from your custom registry, making it easy to integrate and extend functionality.

You have too many registries to manage?

You can use the loader-system-registry-group.md feature to manage multiple registries at once or use built-in groups.

How to create a registry

To show how to create your own registry, go to How to create a registry

Last updated