Code inside Content Posts not working

I am new to Hugo but it seems that my code inside Posts is not working.
When I enter assigning to variable it is just rendered as text.
See attached image.


Go template code can not be added in content files. Only in template files.

Look in to Hugo shortcodes and you find the solution for this.

You might also want to look in to Hugo Markdown render hooks.

Thanks. I already tried with shortcode still no result. I am trying to use ShortCode from ImageProcessing like this:

{{< imgproc Sunset Resize "300x" />}}

and I am getting error:

 execute of template failed: template: shortcodes/imgproc.html:7:16: executing "shortcodes/imgproc.html" at <$img.Resize>: nil pointer evaluating resource.Resource.Resize

You are more likely to receive a prompt and accurate response if you post a link to the public repository for your project.

See Requesting Help.

Let us see your code

Include a link to the source code repository of your project, because we really need the context of seeing your templates and partials to be able to help you. It is trivial to do a quick git clone on your repo, then run hugo server in your project, to help you out. On the other hand, recreating your code from screenshots, or sort of guessing at it, is not.

If you can’t share your repository for whatever reason, consider creating a dummy repo that you can share, which reproduces the problem you’re experiencing.

Your image can’t be found by Hugo. Check file name and location.

I have this image named Sunset.jpg in static/images and in assets/images folders.
I get two different errors:

nil pointer evaluating resource.Resource.Resize
nil pointer evaluating resource.Resource.RelPermalink

Not sure what I am doing wrong here:

{{< imgproc Sunset Resize "300x" />}}

If you look at the first line of the shortcode from the documentation…

You will see:

{{ $img := .Page.Resources.GetMatch (printf "*%s*" (.Get 0)) }}

So the image must be a page resource, which means it must be within a page bundle:

├── posts/
│   └── post-1/
│       ├──
│       └── sunset.jpg

This structure is also described in the image processing documentation.

This does not suit me well, because I am migrating a website from WordPress which has images in folders.
Is there a way to change that code to read from static or assets folder?

Certainly. It’s there for everyone to modify. Otoh: you might be better off adjusting to Hugo’s ways then trying to follow WP customs.

Sure. Just change the first line of the shortcode to:

{{ $img := resources.GetMatch (printf "*%s*" (.Get 0)) }}

And then put your images in the assets directory.

This works on Windows, however when I deployed it to AWS not work anymore.
{{<imgproc "images\photo.jpg" Resize "600x" />}}
Not sure why but on Linux it seems that cannot find photos in assets\images folder

I also have just 1 more issue.
I removed this:

    {{ with .Inner }}
      {{ . }}
    {{ else }}
      .{{ $command }} "{{ $options }}"
    {{ end }}

I want to not display figcaption and this is optional in HTML specification however if I remove this from the template I get a error.
Any solution?
failed to extract shortcode: shortcode ">}}" does not evaluate .Inner or .InnerDeindent, yet a closing tag was provided

Use forward slashes, not backslashes.

Do not self-close the shortcode. Do this:

{{< foo >}}

not this

{{< foo />}}

You are right thanks again.
On Windows only works with backslashes on Linux with forward slashes.

Is there a way to remove this

    {{ with .Inner }}
      {{ . }}
    {{ else }}
      .{{ $command }} "{{ $options }}"
    {{ end }}

without getting error:

failed to extract shortcode: shortcode ">}}" does not evaluate .Inner or .InnerDeindent, yet a closing tag was provided

I only need to make this work to be able to use it?

That is not true. If you use forward slashes in the shortcode’s path parameter, it works on macOs, Linux, and Windows.

As I mentioned above, do not self-close. Do this:

{{< imgproc "images/sunset.jpg" Resize 300x >}}

not this:

{{< imgproc "images/sunset.jpg" Resize 300x /

@jmooring Man thanks for the help. Everything is working now!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.