From 704dd5da68da3dd3835d00b8238af3572710402c Mon Sep 17 00:00:00 2001 From: Joe Mooring Date: Tue, 8 Aug 2023 06:51:53 -0700 Subject: [PATCH] Document the transform.Remarshal template function --- content/en/functions/transform.Remarshal.md | 88 +++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 content/en/functions/transform.Remarshal.md diff --git a/content/en/functions/transform.Remarshal.md b/content/en/functions/transform.Remarshal.md new file mode 100644 index 000000000..e1605197f --- /dev/null +++ b/content/en/functions/transform.Remarshal.md @@ -0,0 +1,88 @@ +--- +title: transform.Remarshal +description: Marshals a string of serialized data, or a map, into a string of serialized data in the specified format. +categories: [functions] +menu: + docs: + parent: functions +keywords: [] +signature: [ transform.Remarshal FORMAT INPUT ] +--- + +The FORMAT must be one of `json`, `toml`, `yaml`, or `xml`. If the INPUT is a string of serialized data, it must be valid JSON, TOML, YAML, or XML. + +{{% note %}} +This function is primarily a helper for Hugo's documentation, used to convert configuration and front matter examples to JSON, TOML, and YAML. + +This is not a general purpose converter, and may change without notice if required for Hugo's documentation site. +{{% /note %}} + +Example 1 +: Convert a string of TOML to JSON. + +```go-html-template +{{ $s := ` + baseURL = 'https://example.org/' + languageCode = 'en-US' + title = 'ABC Widgets' +`}} +
{{ transform.Remarshal "json" $s }}
+``` + +Resulting HTML: + +```html +
{
+   "baseURL": "https://example.org/",
+   "languageCode": "en-US",
+   "title": "ABC Widgets"
+}
+
+``` + +Rendered in browser: + +```text +{ + "baseURL": "https://example.org/", + "languageCode": "en-US", + "title": "ABC Widgets" +} +``` + +Example 2 +: Convert a map to YAML. + +```go-html-template +{{ $m := dict + "a" "Hugo rocks!" + "b" (dict "question" "What is 6x7?" "answer" 42) + "c" (slice "foo" "bar") +}} +
{{ transform.Remarshal "yaml" $m }}
+``` + +Resulting HTML: + +```html +
a: Hugo rocks!
+b:
+  answer: 42
+  question: What is 6x7?
+c:
+- foo
+- bar
+
+``` + +Rendered in browser: + +```text +a: Hugo rocks! +b: + answer: 42 + question: What is 6x7? +c: +- foo +- bar +```