Skip to main content
Help improve this documentation

This documentation is still new and evolving. If you spot any mistakes, unclear explanations, or missing details, please open an issue.

Your feedback helps us improve!

template operatorsโ€‹

This page lists all operators available in the template sub-package of ro.

Installโ€‹

First, import the sub-package in your project:

go get -u github.com/samber/ro/plugins/template
  • Applies text template to values.

    import (
    "github.com/samber/ro"
    rotemplate "github.com/samber/ro/plugins/template"
    )

    type User struct {
    Name string
    Age int
    }

    obs := ro.Pipe[User, string](
    ro.Just(User{Name: "Alice", Age: 30}),
    rotemplate.TextTemplate[User]("Hello {{.Name}}, you are {{.Age}} years old"),
    )

    sub := obs.Subscribe(ro.PrintObserver[string]())
    defer sub.Unsubscribe()

    // Next: Hello Alice, you are 30 years old
    // Completed
    Similar:
    Prototype:
    func TextTemplate[T any](template string)
  • Applies HTML template to values.

    import (
    "github.com/samber/ro"
    rotemplate "github.com/samber/ro/plugins/template"
    )

    type Item struct {
    Name string
    Price float64
    }

    obs := ro.Pipe[Item, string](
    ro.Just(Item{Name: "Apple", Price: 1.99}),
    rotemplate.HTMLTemplate[Item]("<h1>{{.Name}}</h1><p>Price: ${{.Price}}</p>"),
    )

    sub := obs.Subscribe(ro.PrintObserver[string]())
    defer sub.Unsubscribe()

    // Next: <h1>Apple</h1><p>Price: $1.99</p>
    // Completed
    Similar:
    Prototype:
    func HTMLTemplate[T any](template string)