Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

v1 source pure

Package dynreplacer provides a simple template engine for handling dynamic content replacement. It is similar to stri...

Readme View source

gno.land/p/moul/dynreplacer/v1

Simple template engine for dynamic content replacement.


Part of moul/gno-contracts — moul's versioned gno.land contracts. See the repository for the full catalog, build/test tooling, and usage.

Provenance: imported — see https://github.com/moul/gno-contracts/pull/2 for context and metadata.

⚠️ Disclaimer: provided as-is, without warranty; not security-audited. Full disclaimer: DISCLAIMER.

Overview

Package dynreplacer provides a simple template engine for handling dynamic content replacement. It is similar to strings.Replacer but with lazy execution of replacements, making it more optimization-friendly in several cases. While strings.Replacer requires all replacement values to be computed upfront, dynreplacer only executes the callback functions for placeholders that actually exist in the template, avoiding unnecessary computations.

The package ensures efficient, non-recursive replacement of placeholders in a single pass. This lazy evaluation approach is particularly beneficial when: - Some replacement values are expensive to compute - Not all placeholders are guaranteed to be present in the template - Templates are reused with different content

Example usage:

Example
1r := dynreplacer.New(
2    dynreplacer.Pair{":name:", func() string { return "World" }},
3    dynreplacer.Pair{":greeting:", func() string { return "Hello" }},
4)
5result := r.Replace("Hello :name:!") // Returns "Hello World!"

The replacer caches computed values, so subsequent calls with the same placeholder will reuse the cached value instead of executing the callback again:

Example
1r := dynreplacer.New()
2r.RegisterCallback(":expensive:", func() string { return "computed" })
3r.Replace("Value1: :expensive:") // Computes the value
4r.Replace("Value2: :expensive:") // Uses cached value
5r.ClearCache()                   // Force re-computation on next use

Functions 1

func New

1func New(pairs ...Pair) *Replacer
source

New creates a new Replacer instance with optional initial replacements. It accepts pairs where each pair consists of a placeholder string and its corresponding callback function.

Example:

Example
1New(
2    Pair{":name:", func() string { return "World" }},
3    Pair{":greeting:", func() string { return "Hello" }},
4)

Types 2

type Pair

struct
1type Pair struct {
2	Placeholder string
3	Callback    func() string
4}
source

Pair represents a placeholder and its callback function

type Replacer

struct
1type Replacer struct {
2	callbacks    map[string]func() string
3	cachedValues map[string]string
4}
source

Replacer manages dynamic placeholders, their associated functions, and cached values.

Methods on Replacer

func ClearCache

method on Replacer
1func (r *Replacer) ClearCache()
source

ClearCache clears all cached values, forcing re-computation on next Replace.

func RegisterCallback

method on Replacer
1func (r *Replacer) RegisterCallback(placeholder string, callback func() string)
source

RegisterCallback associates a placeholder with a function to generate its content.

func Replace

method on Replacer
1func (r *Replacer) Replace(layout string) string
source

Replace processes the given layout, replacing placeholders with cached or newly computed values.

Imports 1

  • strings stdlib

Source Files 3