Encoding

The Encoding registry offers methods for encoding and decoding data in different formats, allowing for flexible data representation and storage within your templates.

You can easily import all the functions from the encoding registry by including the following import statement in your code

import "github.com/go-sprout/sprout/registry/encoding"

base64Encode

The function encodes a given string into its Base64 representation, converting the data into a text format suitable for transmission or storage in systems that support Base64 encoding.

Signature

Base64Encode(s string) string

Must version

{{ "Hello World" | base64Encode }} // Output: "SGVsbG8gV29ybGQ="

base64Decode

The function decodes a Base64 encoded string back to its original form. If the input string is not valid Base64, it returns an error message.

Signature

 Base64Decode(s string) string

Must version

{{ "SGVsbG8gV29ybGQ=" | base64Decode }} // Output: "Hello World"

base32Encode

The function encodes a given string into its Base32 representation, converting the data into a text format that is compatible with systems supporting Base32 encoding.

Signature

Base32Encode(s string) string

Must version

{{ "Hello World" | base32Encode }} // Output: "JBSWY3DPEBLW64TMMQQQ===="

base32Decode

The function decodes a Base32 encoded string back to its original form. If the input is not valid Base32, it returns an error message.

Signature

Base32Decode(s string) string

Must version

{{ "JBSWY3DPEBLW64TMMQQQ====" | base32Decode }} // Output: "Hello World"

fromJson / mustFromJson

The function converts a JSON string into a corresponding Go data structure, enabling easy manipulation of the JSON data in a Go environment.

Signature

FromJson(v string) any
MustFromJson(v string) (any, error)

Must version

{{ '{"name":"John", "age":30}' | fromJson }} // Output: map[name:John age:30]

toJson / mustToJson

The function converts a Go data structure into a JSON string, allowing the data to be easily serialized for storage, transmission, or further processing.

Signature

ToJson(v any) string
MustToJson(v any) (string, error)

Must version

{{ $d := dict "key1" "value1" "key2" "value2" "key3" "value3" }}
{{ toJson $d }} // Output: {"key1":"value1","key2":"value2","key3":"value3"}

toPrettyJson / mustToPrettyJson

The function converts a Go data structure into a pretty-printed JSON string, formatting the output with indentation and line breaks for better readability.

Signature

ToPrettyJson(v any) string
MustToPrettyJson(v any) (string, error)

Must version

{{ $d := dict "key1" "value1" "key2" "value2" "key3" "value3" }}
{{ toPrettyJson $d }} // Output: "{\n  \"key1\": \"value1\",\n  \"key2\": \"value2\",\n  \"key3\": \"value3\"\n}"

toRawJson / mustToRawJson

The function converts a Go data structure into a JSON string without escaping HTML characters, preserving the raw content as it is.

Signature

ToRawJson(v any) string
MustToRawJson(v any) (string, error)

Must version

{{ $d := dict "content" "<p>Hello World</p>" }}
{{ toRawJson $d }} // Output: {"content":"<p>Hello World</p>"}

fromYaml / mustFromYaml

The function deserializes a YAML string into a Go map, allowing the structured data from YAML to be used and manipulated within a Go program.

Signature

FromYAML(str string) any
MustFromYAML(v string) (any, error)

Must version

{{ "name: John Doe\nage: 30" | fromYaml }} // Output: map[name:John Doe age:30]

toYaml / mustToYaml

The function serializes a Go data structure into a YAML string, converting the data into a format suitable for YAML representation.

Signature

ToYAML(v any) string
MustToYAML(v any) (string, error)

Must version

{{ $d := dict "name" "John Doe" "age" 30 }}
{{ toYaml $d }} // Output: name: John Doe\nage: 30

Last updated

#18: v0.5 loaders with handlers and registries

Change request updated