From 6e36a9db0c5a7bd11b9833558f8478ce16d82f0c Mon Sep 17 00:00:00 2001 From: NotZippy Date: Fri, 2 Oct 2015 08:30:21 -0700 Subject: [PATCH] Add dictionary function to be passed into a template Allows templates to dynamically build maps. Example usage: Creating and passing a map to a subtemplate while in a range on the parent. --- content/templates/functions.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/content/templates/functions.md b/content/templates/functions.md index d27e45035..88129aa1f 100644 --- a/content/templates/functions.md +++ b/content/templates/functions.md @@ -50,6 +50,29 @@ e.g. // Outputs Tags: tag1, tag2 and tag3 +### dict +Creates a dictionary (map[string, interface{}), expects parameters added in value:object fasion. +Invalid combinations like keys that are not strings or uneven number of parameters, will result in an exception thrown +Useful for passing maps to partials when adding to a template. + +e.g. Pass into "foo.html" a map with the keys "important, content" + + {{$important := .Site.Params.SomethingImportant }} + {{range .Site.Params.Bar}} + {{partial "foo" (dict "content" . "important" $important)}} + {{end}} + +"foo.html" + + Important {{.important}} + {{.content}} + + +or Create a map on the fly to pass into + + {{partial "foo" (dict "important" "Smiles" "content" "You should do more")}} + + ### echoParam Prints a parameter if it is set.