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

{{ "Hello World!" | base64Encode }} // Output: "SGVsbG8gV29ybGQh"

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

{{ "SGVsbG8gV29ybGQh" | 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

{{ "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

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

fromJSON

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

Signature

{{ `{"name":"John", "age":30}` | fromJSON }} // Output: map[age:30 name:John]
{{ "{\\invalid" | fromJSON }} // Error

toJSON

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

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

toPrettyJSON

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

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

toRawJSON

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

Signature

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

fromYAML

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


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

toYAML

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

Signature

{{- $d := dict "name" "John Doe" "age" 30 -}}
{{ $d | toYAML }} // Output: age: 30\nname: John Doe

toIndentYAML

The function serializes a Go data structure into a YAML string, converting the data into a format suitable for YAML representation. In addition to toYAML, toIndentYAML takes a parameter to define the indentation width in spaces.

Signature

{{ $person := dict "name" "John Doe" "age" 30 "location" (dict "country" "US" "planet" "Earth") }}
{{ $person | toIndentYAML 2 }} // Output: \nage: 30\nlocation:\n  country: US\n  planet: Earth\nname: John Doe

Last updated