DEV Community

Cover image for Introducing Sprout 1.0 🌱💜: A New Era for Go Templates
42Atomys
42Atomys

Posted on

Introducing Sprout 1.0 🌱💜: A New Era for Go Templates

#go

Hey, Go Developers!

I'm thrilled to announce Sprout 1.0, the ultimate library to take your Go templates to the next level. Whether you’re building something simple or diving into complex templates, Sprout has your back. With powerful features and a focus on ease of use, Sprout brings a new era of possibilities to your Go projects.
Let’s explore why Sprout might just be the best tool you didn’t know you needed. If you’ve been searching for a library that balances power, speed, and simplicity, this is it.

My first v1.0.0 release of all time, thanks to LLama3.3 (local install) to have helped me to write this post 😳


Why Sprout?

Sprout was born out of a need for a more powerful, flexible, and user-friendly templating library for Go. Built on the foundation of Sprig, Sprout adds:

  • More Functions: A robust set of tools (200+) to handle everything from string manipulation to advanced math, helping you save time and effort.
  • Error Handling: Fully integrated with Go’s native error-handling mechanisms, making debugging and resilience a breeze.
  • Customizable Loading: Load only what you need, making your apps lighter and faster without unnecessary overhead.
  • Consistent Naming: No more cryptic names—Sprout keeps it clear and easy to use, so you can focus on building.
  • Comprehensive Docs: Dive into detailed examples and guides to master every feature and unlock the library’s full potential.

With Sprout, you’re not just using a library; you’re adding a toolkit designed to simplify and amplify your Go templating experience.


Getting Started

Here’s how you can start using Sprout in your projects:

$ go get github.com/go-sprout/sprout
Enter fullscreen mode Exit fullscreen mode

Once installed, you can use it like this:

package main

import (
    "log"
    "os"
    "text/template"

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

// This example demonstrates the basic usage of Sprout to enhance Go templates.
// - It uses the "toUpper" function from the "strings" registry to convert text to uppercase.
// - The handler builds the function map and integrates it with the Go "text/template" package.
func main() {
    // Initialize a Sprout handler with the strings registry.
    handler := sprout.New(
        sprout.WithRegistries(strings.NewRegistry()),
    )

    // Build the function map from the handler.
    funcs := handler.Build()

    // Define a template string that uses the "toUpper" function.
    tmplString := `{{ toUpper "hello, sprout!" }}`

    // Parse the template with the custom functions.
    tmpl, err := template.New("example").Funcs(funcs).Parse(tmplString)
    if err != nil {
        log.Fatalf("Error parsing template: %v", err)
    }

    // Execute the template and print the output.
    if err := tmpl.Execute(os.Stdout, nil); err != nil {
        log.Fatalf("Error executing template: %v", err)
    }
}
Enter fullscreen mode Exit fullscreen mode

This simple example shows how Sprout’s toUpper function makes converting text to uppercase a breeze. With just a few lines, you can extend your templates with powerful and efficient functionality. It’s that easy to start making your projects shine.


Deprecation and Alias Example

One of Sprout's coolest features is its ability to handle deprecated functions and aliases. This means you can rename or replace functions without breaking old templates. Here’s how it works:

package main

import (
    "log"
    "os"
    "text/template"

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

func main() {
    // Initialize the handler with deprecation notices and aliases.
    handler := sprout.New(
        // Register the standard "strings" functions, including operations like toUpper.
        sprout.WithRegistries(strings.NewRegistry()),

        // Add a deprecation notice for "uppercase" to guide users to the new "toUpper" function.
        sprout.WithNotices(
            sprout.NewDeprecatedNotice("uppercase", "please use `toUpper` instead"),
        ),

        // Create an alias so "uppercase" still works but maps to the new "toUpper" function.
        sprout.WithAlias("toUpper", "uppercase"),
    )

    // Define a template string that uses the deprecated "uppercase" function.
    tmplString := `{{ uppercase "Hello, Sprout!" }}`

    // Parse the template with the custom functions.
    tmpl, err := template.New("example").Funcs(handler.Build()).Parse(tmplString)
    if err != nil {
        log.Fatalf("Error parsing template: %v", err)
    }

    // Execute the template and print the output.
    if err := tmpl.Execute(os.Stdout, nil); err != nil {
        log.Fatalf("Error executing template: %v", err)
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, uppercase is deprecated and redirects to toUpper. A warning is displayed to let you know that the old function will be removed in the future. This feature ensures smooth transitions and future-proof templates without disruption.


What Makes Sprout Stand Out?

  1. Active Development: Sprout is constantly evolving with regular updates and improvements based on user feedback.
  2. Lightweight and Fast: Minimal dependencies ensure your apps stay fast and efficient.
  3. Community-Driven: Feedback from developers like you shapes Sprout’s future, making it a library truly built for the community.
  4. Built for Scalability: Whether you’re managing small projects or large-scale applications, Sprout adapts to your needs effortlessly.

Sprout isn’t just another templating library—it’s a partner in your development journey, simplifying workflows and unlocking creativity.


Want to Contribute or Learn More?

Whether you’re here to learn or contribute, Sprout welcomes everyone to join its growing community. Together, we can shape the future of Go templating.


Join the Conversation

What do you think of Sprout 1.0? Have ideas or features you’d like to see? Drop a comment below or contribute on GitHub.
Together, let’s make Go templates better for everyone!
I truly think Go templates need some improvement, and I want Sprout to be the place for that evolution!
Share your experiences, showcase your projects, and inspire others to build amazing things with Sprout.

Happy Coding! 🚀💜

Top comments (1)

Collapse
 
42atomys profile image
42Atomys

This is my first v1.0.0 release on the Open Source ecosystem, I'm proud of my work and I hope see you on the OSS ! 🥰💜