Just need a a little syntax help (btw is there go documentation that might expand on this beyond Hugo docs??)
I have a shortcode. It currently reads a single parameter (class) but I want it to read as many parameters as given by the document and put them into the div class tag I am creating.
How do you “loop through” all parameters that might be given (in this case a list of parameters that will become html classes).
<div class={{ .Get 0 }}} > #should loop through all given parameters and add them all to this div class tag.
{{ .Inner }}
</div>
The code in document would look like this
{{% format class1 class2 class3 etc %}}
A bunc of text here.
{{% /format %}}
and html should be
<div class= class1 class2 class3 etc>
A bunch of text here
</dv>
I am assuming this isn’t really a “hugo” syntax/construction but rather Go? In which case a little study of Go would allow me to figure out this and more complicated shortcodes and templates on my own? If that is true than can anyone suggest a nice, to the point, Go primer.
Now I want to create multiple classes but with named parameters instead of a list.
I have this
but now I am adding a “width” parameter which I want to use to append a second class to my list of classes in my html tag.
Let’ make this generic. How to add any number of named parameters. Can “range” walk over a list(array) of named parameters instead of just the index of the parameters like I have now. Wild guess a kinda get/range combo?
Still a bit confused. Where, in what you gave, do I specify which of the parameter/values I want to pull/filter from the complete list of parameters? In my case the values associated with named parameters “position” and “width”. Sorry, guess I just need you to spell it out for me with my example.
I have other parameters and order is never the same so I want to pick these solely based on parameter name. Not a problem if only one (I just use Get) but with more that one I am confused about the syntax.
My complete shortcode image.html currently looks like this. Just don’t get how to cram .Get “width” and have it’s value placed after the other class from parameter “position”.
<figure
{{ with .Get "position" }}class="{{.}}"{{ end }}>
{{ with .Get "link"}}<a href="{{.}}">{{ end }}
<img src="{{ .Page.Params.imagespath }}{{ .Get "filename" }}"{{ if or (.Get "alt") (.Get "caption") }}alt="{{ with .Get "alt"}}{{.}}{{else}}{{ .Get "caption" }}{{ end }}"{{ end }} />
{{ if .Get "link"}}</a>{{ end }}
{{ if or (or (.Get "title") (.Get "caption")) (.Get "attr")}}
<figcaption>
{{ if or (.Get "caption") (.Get "attr")}}<p>
{{ .Get "caption" }}
{{ with .Get "attrlink"}}<a href="{{.}}"> {{ end }}
{{ .Get "attr" }}
{{ if .Get "attrlink"}}</a> {{ end }}
</p> {{ end }}
</figcaption>
{{ end }}
</figure>
{{% image filename="01-000.jpg" caption="" width="small" position="left" %}}
<figure class="
{{ $key, $val range .Params }
{{ if and (ne $key "filename") (ne $key "caption") }}
{ $val }
{{ end }}
{{ end }}
">
There’s an invisible whitespace above that should separate the classes, I hope …
The above feels slightly hackish. For many params, it may be better to use positional params and reserve the first n for name, caption … and the rest for CSS classes.
ERROR: 2015/05/01 template: shortcodes/image.html:3: too many declarations in command
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x20 pc=0x480526]
Couple reasons why I am trying not to force positional parameters. 1. parameters are optional, 2. Editors will be writing the moustache code so don’t want to force an order on them which they will invariably screw up.
Other than this error can it be written without the ne. in other words the reverse to look for the two parameter names instead of just excluding others which may are may not be there.
like
and $key="position" or $key="width"
I’d eventually like to figure this out for this and other shortcodes. So unless you are keen on figuring this out for yourself don’t waste too much time. I can do a custom shortcode for now that will cover the width. Too maybe someone else has an idea how to tackle this. @spf13 maybe?