# Parsing (?) error in shortcode

**URL:** <https://discourse.gohugo.io/t/parsing-error-in-shortcode/11143>\
**Category:** support\
**Created:** [March 22, 2018, 1:02pm UTC](https://discourse.gohugo.io/t/parsing-error-in-shortcode/11143 "2018-03-22T13:02:18Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidsneighbour](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/davidsneighbour/32/24507_2.png) [@davidsneighbour](https://discourse.gohugo.io/u/davidsneighbour)\
**Post date:** [March 22, 2018, 1:02pm UTC](https://discourse.gohugo.io/t/parsing-error-in-shortcode/11143/1 "2018-03-22T13:02:18Z")

</div>

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:

```auto

{{ 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/](http://gohugo.io/content-management/image-processing/)

---

<div class="post-metadata">

**Author:** ![davidsneighbour](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/davidsneighbour/32/24507_2.png) [@davidsneighbour](https://discourse.gohugo.io/u/davidsneighbour)\
**Post date:** [March 22, 2018, 1:09pm UTC](https://discourse.gohugo.io/t/parsing-error-in-shortcode/11143/2 "2018-03-22T13:09:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![regis](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/regis/32/4958_2.png) [@regis](https://discourse.gohugo.io/u/regis)\
**Post date:** [March 22, 2018, 3:35pm UTC](https://discourse.gohugo.io/t/parsing-error-in-shortcode/11143/3 "2018-03-22T15:35:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![davidsneighbour](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/davidsneighbour/32/24507_2.png) [@davidsneighbour](https://discourse.gohugo.io/u/davidsneighbour)\
**Post date:** [March 23, 2018, 7:49am UTC](https://discourse.gohugo.io/t/parsing-error-in-shortcode/11143/4 "2018-03-23T07:49:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![regis](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/regis/32/4958_2.png) [@regis](https://discourse.gohugo.io/u/regis)\
**Post date:** [March 23, 2018, 12:08pm UTC](https://discourse.gohugo.io/t/parsing-error-in-shortcode/11143/5 "2018-03-23T12:08:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![davidsneighbour](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/davidsneighbour/32/24507_2.png) [@davidsneighbour](https://discourse.gohugo.io/u/davidsneighbour)\
**Post date:** [March 23, 2018, 12:14pm UTC](https://discourse.gohugo.io/t/parsing-error-in-shortcode/11143/6 "2018-03-23T12:14:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![davidsneighbour](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/davidsneighbour/32/24507_2.png) [@davidsneighbour](https://discourse.gohugo.io/u/davidsneighbour)\
**Post date:** [March 23, 2018, 12:44pm UTC](https://discourse.gohugo.io/t/parsing-error-in-shortcode/11143/7 "2018-03-23T12:44:57Z")

</div>

Nope, 't wasn’t the calling function.

I made a branch with testcases at [https://bitbucket.org/pkollitsch/samui-samui.de/src/working-figuretest/](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.

---

<div class="post-metadata">

**Author:** ![regis](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/regis/32/4958_2.png) [@regis](https://discourse.gohugo.io/u/regis)\
**Post date:** [March 23, 2018, 1:54pm UTC](https://discourse.gohugo.io/t/parsing-error-in-shortcode/11143/8 "2018-03-23T13:54:31Z")

</div>

Let me take a look!

---

<div class="post-metadata">

**Author:** ![regis](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/regis/32/4958_2.png) [@regis](https://discourse.gohugo.io/u/regis)\
**Post date:** [March 23, 2018, 2:36pm UTC](https://discourse.gohugo.io/t/parsing-error-in-shortcode/11143/9 "2018-03-23T14:36:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![davidsneighbour](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/davidsneighbour/32/24507_2.png) [@davidsneighbour](https://discourse.gohugo.io/u/davidsneighbour)\
**Post date:** [March 23, 2018, 3:09pm UTC](https://discourse.gohugo.io/t/parsing-error-in-shortcode/11143/10 "2018-03-23T15:09:38Z")

</div>

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 🙂

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 😃

---

<div class="post-metadata">

**Author:** ![davidsneighbour](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/davidsneighbour/32/24507_2.png) [@davidsneighbour](https://discourse.gohugo.io/u/davidsneighbour)\
**Post date:** [March 23, 2018, 3:12pm UTC](https://discourse.gohugo.io/t/parsing-error-in-shortcode/11143/11 "2018-03-23T15:12:39Z")

</div>

Final shortcode:

```auto
{{ 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 }}

```

---

<div class="post-metadata">

**Author:** ![regis](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/regis/32/4958_2.png) [@regis](https://discourse.gohugo.io/u/regis)\
**Post date:** [March 23, 2018, 4:03pm UTC](https://discourse.gohugo.io/t/parsing-error-in-shortcode/11143/12 "2018-03-23T16:03:24Z")

</div>

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

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