I am trying to combine my “old” figure shortcode with the shortcode presented in the documentation for image processing [1]
My shortcode looks like this now:
{{ if (.Get "name") }}
{{ $original := .Page.Resources.GetMatch (printf "%s*" (.Get "name")) }}
{{ $command := .Get "command" }}
{{ $options := .Get "options" }}
{{ if eq $command "Fit"}}
{{ .Scratch.Set "image" ($original.Fit $options) }}
{{ else if eq $command "Resize"}}
{{ .Scratch.Set "image" ($original.Resize $options) }}
{{ else if eq $command "Fill"}}
{{ .Scratch.Set "image" ($original.Fill $options) }}
{{ else }}
{{ errorf "Invalid image processing command: Must be one of Fit, Fill or Resize."}}
{{ end }}
{{ $image := .Scratch.Get "image" }}
{{ end }}
<figure {{ with .Get "class" }}class="{{.}}"{{ end }}>
{{ with .Get "link"}}<a href="{{.}}">{{ end }}
<img
src='{{ if (.Get "name") }}{{ $image.RelPermalink }}{{ else }}{{ .Get "src" }}{{ end }}'
{{ if (.Get "name") }}width="{{ $image.Width }}" {{ else }}{{ with "width" }}width="{{.}}" {{ end }}{{ end }}
{{ if (.Get "name") }}height="{{ $image.Height }}" {{ else }}{{ with "height" }}height="{{.}}" {{ end }}{{ end }}
{{ if or (.Get "alt") (.Get "caption") }}alt="{{ with .Get "alt"}}{{.}}{{else}}{{ .Get "caption" }}{{ end }}" {{ end }}
>
{{ if .Get "link"}}</a>{{ end }}
{{ with .Inner }}
<figcaption>{{ . }}</figcaption>
{{ end }}
</figure>
The line creating problems is the one starting with src, it throws an ERROR 2018/03/22 19:55:01 shortcodes/figure2.html : template: shortcodes/figure2.html:26: undefined variable "$image"
What might be the issue? I first thought that all the " were irritating the parser, but even with src='bla' it’s throwing errors.
I tested around and found, that {{ if (.Get “name”) }} does not return true. So the $image is indead not found. As soon as I remove the if around the image processing it gets processed properly. So the name value does exist and is right. It’s just not found if or with. I made it into two separate shortcodes now, one for resources and one for a path.
I wonder if those functions aren’t available to resources.
I’ll upload a testcase in some minutes… need to figure out how to get Netlify not to put it live. Probably via branch… I am having an idea: the shortcode got called via {{% so the .Inner gets parsed for the caption. I’ll add a version that checks for all ways to implement it.
OK… Bottom line, you cannot declare this variable $image inside the if block, you have to define it outside of it and everything works fine.
Not being able to override a previously declared variable is very well know fact, but I was not aware of this other limitation, but now it makes sense…
I created a PR (with the wrong bitbucket accoung ) on your branch so you can have a look at my suggested changes.