# Code inside Content Posts not working

**URL:** https://discourse.gohugo.io/t/code-inside-content-posts-not-working/44855
**Category:** support
**Created:** [June 18, 2023, 6:50am UTC](https://discourse.gohugo.io/t/code-inside-content-posts-not-working/44855 "2023-06-18T06:50:35Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![PeroDjetlic](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/perodjetlic/32/19671_2.png) [@PeroDjetlic](https://discourse.gohugo.io/u/PeroDjetlic)
#### Post date: [June 18, 2023, 6:50am UTC](https://discourse.gohugo.io/t/code-inside-content-posts-not-working/44855/1 "2023-06-18T06:50:35Z")

</div>

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.

![Untitled](https://canada1.discourse-cdn.com/flex036/uploads/gohugo/original/3X/9/2/928231597a3f41153035a7b5da12b9e462449d1a.png)

---

<div class="post-metadata">

### Author: ![frjo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/frjo/32/18626_2.png) [@frjo](https://discourse.gohugo.io/u/frjo)
#### Post date: [June 18, 2023, 8:22am UTC](https://discourse.gohugo.io/t/code-inside-content-posts-not-working/44855/2 "2023-06-18T08:22:09Z")

</div>

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.

> **[Shortcodes](https://gohugo.io/content-management/shortcodes/)**
>
> Shortcodes are simple snippets inside your content files calling built-in or custom templates.

---

<div class="post-metadata">

### Author: ![frjo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/frjo/32/18626_2.png) [@frjo](https://discourse.gohugo.io/u/frjo)
#### Post date: [June 18, 2023, 8:23am UTC](https://discourse.gohugo.io/t/code-inside-content-posts-not-working/44855/3 "2023-06-18T08:23:27Z")

</div>

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

> **[Markdown Render Hooks](https://gohugo.io/templates/render-hooks/)**
>
> Render Hooks allow custom templates to override markdown rendering functionality.

---

<div class="post-metadata">

### Author: ![PeroDjetlic](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/perodjetlic/32/19671_2.png) [@PeroDjetlic](https://discourse.gohugo.io/u/PeroDjetlic)
#### Post date: [June 18, 2023, 11:21am UTC](https://discourse.gohugo.io/t/code-inside-content-posts-not-working/44855/4 "2023-06-18T11:21:07Z")

</div>

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

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

```

and I am getting error:

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

```

---

<div class="post-metadata">

### Author: ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)
#### Post date: [June 18, 2023, 2:06pm UTC](https://discourse.gohugo.io/t/code-inside-content-posts-not-working/44855/5 "2023-06-18T14:06:46Z")

</div>

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](https://discourse.gohugo.io/t/requesting-help/9132).

> 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.

---

<div class="post-metadata">

### Author: ![chrillek](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/chrillek/32/19797_2.png) [@chrillek](https://discourse.gohugo.io/u/chrillek)
#### Post date: [June 18, 2023, 3:40pm UTC](https://discourse.gohugo.io/t/code-inside-content-posts-not-working/44855/6 "2023-06-18T15:40:08Z")

</div>

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

---

<div class="post-metadata">

### Author: ![PeroDjetlic](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/perodjetlic/32/19671_2.png) [@PeroDjetlic](https://discourse.gohugo.io/u/PeroDjetlic)
#### Post date: [June 18, 2023, 6:02pm UTC](https://discourse.gohugo.io/t/code-inside-content-posts-not-working/44855/7 "2023-06-18T18:02:29Z")

</div>

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

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

```

Not sure what I am doing wrong here:

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

```

---

<div class="post-metadata">

### Author: ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)
#### Post date: [June 18, 2023, 6:14pm UTC](https://discourse.gohugo.io/t/code-inside-content-posts-not-working/44855/8 "2023-06-18T18:14:05Z")

</div>

If you look at the first line of the shortcode from the documentation…  
[https://github.com/gohugoio/hugoDocs/blob/master/layouts/shortcodes/imgproc.html#L1](https://github.com/gohugoio/hugoDocs/blob/master/layouts/shortcodes/imgproc.html#L1)

You will see:

```plaintext
{{ $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](https://gohugo.io/content-management/page-bundles/):

```plaintext
content/
├── posts/
│ └── post-1/
│ ├── index.md
│ └── sunset.jpg
└── _index.md

```

This structure is also described in the [image processing documentation](https://gohugo.io/content-management/image-processing/#page-resources).

---

<div class="post-metadata">

### Author: ![PeroDjetlic](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/perodjetlic/32/19671_2.png) [@PeroDjetlic](https://discourse.gohugo.io/u/PeroDjetlic)
#### Post date: [June 18, 2023, 6:44pm UTC](https://discourse.gohugo.io/t/code-inside-content-posts-not-working/44855/9 "2023-06-18T18:44:42Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![chrillek](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/chrillek/32/19797_2.png) [@chrillek](https://discourse.gohugo.io/u/chrillek)
#### Post date: [June 18, 2023, 7:47pm UTC](https://discourse.gohugo.io/t/code-inside-content-posts-not-working/44855/10 "2023-06-18T19:47:41Z")

</div>

> [@PeroDjetlic](#):
>
> 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.

---

<div class="post-metadata">

### Author: ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)
#### Post date: [June 18, 2023, 7:51pm UTC](https://discourse.gohugo.io/t/code-inside-content-posts-not-working/44855/11 "2023-06-18T19:51:07Z")

</div>

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

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

```

And then put your images in the `assets` directory.

---

<div class="post-metadata">

### Author: ![PeroDjetlic](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/perodjetlic/32/19671_2.png) [@PeroDjetlic](https://discourse.gohugo.io/u/PeroDjetlic)
#### Post date: [June 19, 2023, 6:05am UTC](https://discourse.gohugo.io/t/code-inside-content-posts-not-working/44855/12 "2023-06-19T06:05:04Z")

</div>

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:

```auto
 <figcaption>
  <small>
    {{ with .Inner }}
      {{ . }}
    {{ else }}
      .{{ $command }} "{{ $options }}"
    {{ end }}
  </small>
  </figcaption>

```

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`

---

<div class="post-metadata">

### Author: ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)
#### Post date: [June 19, 2023, 6:36am UTC](https://discourse.gohugo.io/t/code-inside-content-posts-not-working/44855/13 "2023-06-19T06:36:24Z")

</div>

Use forward slashes, not backslashes.

Do not self-close the shortcode. Do this:

```auto
{{< foo >}}

```

not this

```auto
{{< foo />}}

```

---

<div class="post-metadata">

### Author: ![PeroDjetlic](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/perodjetlic/32/19671_2.png) [@PeroDjetlic](https://discourse.gohugo.io/u/PeroDjetlic)
#### Post date: [June 19, 2023, 6:45am UTC](https://discourse.gohugo.io/t/code-inside-content-posts-not-working/44855/14 "2023-06-19T06:45:53Z")

</div>

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

Is there a way to remove this

```auto
<figcaption>
  <small>
    {{ with .Inner }}
      {{ . }}
    {{ else }}
      .{{ $command }} "{{ $options }}"
    {{ end }}
  </small>
  </figcaption>

```

without getting error:

```auto
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?

---

<div class="post-metadata">

### Author: ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)
#### Post date: [June 19, 2023, 7:06am UTC](https://discourse.gohugo.io/t/code-inside-content-posts-not-working/44855/15 "2023-06-19T07:06:02Z")

</div>

> [@PeroDjetlic](#):
>
> On Windows only works with backslashes

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

> [@PeroDjetlic](#):
>
> Is there a way to remove this

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

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

```

not this:

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

```

---

<div class="post-metadata">

### Author: ![PeroDjetlic](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/perodjetlic/32/19671_2.png) [@PeroDjetlic](https://discourse.gohugo.io/u/PeroDjetlic)
#### Post date: [June 19, 2023, 8:35am UTC](https://discourse.gohugo.io/t/code-inside-content-posts-not-working/44855/16 "2023-06-19T08:35:03Z")

</div>

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

---

<div class="post-metadata">

### Author: ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)
#### Post date: [June 21, 2023, 8:35am UTC](https://discourse.gohugo.io/t/code-inside-content-posts-not-working/44855/17 "2023-06-21T08:35:07Z")

</div>

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