From fe2ad5bebc0bd19a55f46f7ab83e9862e822ec3a Mon Sep 17 00:00:00 2001 From: spf13 Date: Tue, 25 Feb 2014 23:57:31 -0500 Subject: [PATCH] Shortcode cleanup. Added a ton of tests. Much more flexible with input. Doesn't crash with bad input. Fixed #193 Also added the .Get function to short codes and documentation for that function. --- content/extras/shortcodes.md | 47 +++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/content/extras/shortcodes.md b/content/extras/shortcodes.md index dee8d12da..7e2c548c7 100644 --- a/content/extras/shortcodes.md +++ b/content/extras/shortcodes.md @@ -120,15 +120,24 @@ parameters named parameters work best. **Inside the template** -To access a parameter by either position or name the index method can be used. +To access a parameter by position the .Get method can be used. - {{ index .Params 0 }} - or - {{ index .Params "class" }} + {{ .Get 0 }} -To check if a parameter has been provided use the isset method provided by Hugo. +To access a parameter by name the .Get method should be utilized - {{ if isset .Params "class"}} class="{{ index .Params "class"}}" {{ end }} + {{ .Get "class" }} + + +With is great when the output depends on a parameter being set + + {{ with .Get "class"}} class="{{.}}"{{ end }} + +Get can also be used to check if a parameter has been provided. This is +most helpful when the condition depends on either one value or another... +or both. + + {{ or .Get "title" | .Get "alt" | if }} alt="{{ with .Get "alt"}}{{.}}{{else}}{{.Get "title"}}{{end}}"{{ end }} If a closing shortcode is used, the variable .Inner will be populated with all of the content between the opening and closing shortcodes. If a closing @@ -162,20 +171,19 @@ This would be rendered as {{ % img src="/media/spf13.jpg" title="Steve Francia" %}} Would load the template /layouts/shortcodes/img.html - -
- {{ if isset .Params "link"}}{{ end }} - - {{ if isset .Params "link"}}{{ end }} - {{ if or (or (isset .Params "title") (isset .Params "caption")) (isset .Params "attr")}} +
+ {{ with .Get "link"}}{{ end }} + + {{ if .Get "link"}}{{ end }} + {{ if or (or (.Get "title") (.Get "caption")) (.Get "attr")}}
{{ if isset .Params "title" }} -

{{ index .Params "title" }}

{{ end }} - {{ if or (isset .Params "caption") (isset .Params "attr")}}

- {{ index .Params "caption" }} - {{ if isset .Params "attrlink"}} {{ end }} - {{ index .Params "attr" }} - {{ if isset .Params "attrlink"}} {{ end }} +

{{ .Get "title" }}

{{ end }} + {{ if or (.Get "caption") (.Get "attr")}}

+ {{ .Get "caption" }} + {{ with .Get "attrlink"}} {{ end }} + {{ .Get "attr" }} + {{ if .Get "attrlink"}} {{ end }}

{{ end }}
{{ end }} @@ -203,8 +211,7 @@ Would be rendered as: {{% /highlight %}} The template for this utilizes the following code (already include in hugo) - - {{ $lang := index .Params 0 }}{{ highlight .Inner $lang }} + {{ .Get 0 | highlight .Inner }} And will be rendered as: