From 849f2ed5f34dc6c4e64691b925db000846a2c309 Mon Sep 17 00:00:00 2001 From: digitalcraftsman Date: Sat, 12 Sep 2015 21:45:12 +0200 Subject: [PATCH] Add base64Decode and base64Encode template functions Fixes #1416 --- content/templates/functions.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/content/templates/functions.md b/content/templates/functions.md index 1317ed2ab..d27e45035 100644 --- a/content/templates/functions.md +++ b/content/templates/functions.md @@ -650,3 +650,33 @@ In this version, we are now sorting the tags, converting them to links with "pos {{ end }} `apply` does not work when receiving the sequence as an argument through a pipeline. + +*** + +### base64Encode and base64Decode + +`base64Encode` and `base64Decode` let you easily decode content with a base64 enconding and vice versa through pipes. Let's take a look at an example: + + + {{ "Hello world" | base64Encode }} + + + {{ "SGVsbG8gd29ybGQ=" | base64Decode }} + + +You can also pass other datatypes as argument to the template function which tries +to convert them. Now we use an integer instead of a string: + + + {{ 42 | base64Encode | base64Decode }} + + +**Tip:** Using base64 to decode and encode becomes really powerful if we have to handle +responses of APIs. + + {{ $resp := getJSON "https://api.github.com/repos/spf13/hugo/readme" }} + {{ $resp.content | base64Decode | markdownify }} + + The response of the Github API contains the base64-encoded version of the [README.md](https://github.com/spf13/hugo/blob/master/README.md) in the Hugo repository. +Now we can decode it and parse the Markdown. The final output will look similar to the +rendered version in Github.