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.
This commit is contained in:
spf13 2014-02-25 23:57:31 -05:00
parent 57102aeba6
commit fe2ad5bebc

View File

@ -120,15 +120,24 @@ parameters named parameters work best.
**Inside the template** **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 }} {{ .Get 0 }}
or
{{ index .Params "class" }}
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 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 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" %}} {{ % img src="/media/spf13.jpg" title="Steve Francia" %}}
Would load the template /layouts/shortcodes/img.html Would load the template /layouts/shortcodes/img.html
<!-- image --> <!-- image -->
<figure {{ if isset .Params "class" }}class="{{ index .Params "class" }}"{{ end }}> <figure {{ with .Get "class" }}class="{{.}}"{{ end }}>
{{ if isset .Params "link"}}<a href="{{ index .Params "link"}}">{{ end }} {{ with .Get "link"}}<a href="{{.}}">{{ end }}
<img src="{{ index .Params "src" }}" {{ if or (isset .Params "alt") (isset .Params "caption") }}alt="{{ if isset .Params "alt"}}{{ index .Params "alt"}}{{else}}{{ index .Params "caption" }}{{ end }}"{{ end }} /> <img src="{{ .Get "src" }}" {{ if or (.Get "alt") (.Get "caption") }}alt="{{ with .Get "alt"}}{{.}}{{else}}{{ .Get "caption" }}{{ end }}"{{ end }} />
{{ if isset .Params "link"}}</a>{{ end }} {{ if .Get "link"}}</a>{{ end }}
{{ if or (or (isset .Params "title") (isset .Params "caption")) (isset .Params "attr")}} {{ if or (or (.Get "title") (.Get "caption")) (.Get "attr")}}
<figcaption>{{ if isset .Params "title" }} <figcaption>{{ if isset .Params "title" }}
<h4>{{ index .Params "title" }}</h4>{{ end }} <h4>{{ .Get "title" }}</h4>{{ end }}
{{ if or (isset .Params "caption") (isset .Params "attr")}}<p> {{ if or (.Get "caption") (.Get "attr")}}<p>
{{ index .Params "caption" }} {{ .Get "caption" }}
{{ if isset .Params "attrlink"}}<a href="{{ index .Params "attrlink"}}"> {{ end }} {{ with .Get "attrlink"}}<a href="{{.}}"> {{ end }}
{{ index .Params "attr" }} {{ .Get "attr" }}
{{ if isset .Params "attrlink"}}</a> {{ end }} {{ if .Get "attrlink"}}</a> {{ end }}
</p> {{ end }} </p> {{ end }}
</figcaption> </figcaption>
{{ end }} {{ end }}
@ -203,8 +211,7 @@ Would be rendered as:
{{% /highlight %}} {{% /highlight %}}
The template for this utilizes the following code (already include in hugo) The template for this utilizes the following code (already include in hugo)
{{ .Get 0 | highlight .Inner }}
{{ $lang := index .Params 0 }}{{ highlight .Inner $lang }}
And will be rendered as: And will be rendered as: