Parsing (?) error in shortcode

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.

[1] http://gohugo.io/content-management/image-processing/

The original shortcode worked by the way, so the image should be retrievable.

Everything alright with your “name” attribute? Can you manage to print it in the shortcode?

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.

You have a repo we could look at? Curious myself.

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.

Nope, 't wasn’t the calling function.

I made a branch with testcases at https://bitbucket.org/pkollitsch/samui-samui.de/src/working-figuretest/.

/content/post/2018/03/figuretest3 to figuretest6 are the posts to check. 1 and 2 are using two separate shortcodes.

I also tested the following line:
{{ $original := .Page.Resources.GetMatch (printf "%s*" . ) }} thinking it might be a dot-thing - not working either.

Let me take a look!

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 :roll_eyes:) on your branch so you can have a look at my suggested changes.

I merged it and added one line:

{{ if or ($image) (.Get "src") }}

this way it decides between name or src attribute and all six testcases get displayed :slight_smile:

great.

I don’t get why I can’t define a variable inside of an if block, but I can live with that given that the shortcode now does what it was envisioned for :smiley:

Final shortcode:

{{ if .Get "name" }}
	{{ $original := .Page.Resources.GetMatch (printf "%s*" (.Get "name") ) }}
	{{ if $original }}
		{{ $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 }}
	{{ end }}
{{ end }}
{{ $image := .Scratch.Get "image" }}
{{ if or ($image) (.Get "src") }}
	<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>
{{ end }}

If you don’t have $image (but only .Get "src") then your reference to $image in the shortcode will throw errors :slight_smile:

Wait I get it, you test .Get "name" before using $image.