# Get resource from page bundle OR the assets dir?

**URL:** <https://discourse.gohugo.io/t/get-resource-from-page-bundle-or-the-assets-dir/36340>\
**Category:** support\
**Created:** [January 3, 2022, 2:46pm UTC](https://discourse.gohugo.io/t/get-resource-from-page-bundle-or-the-assets-dir/36340 "2022-01-03T14:46:22Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Georg](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/georg/32/21486_2.png) [@Georg](https://discourse.gohugo.io/u/Georg)\
**Post date:** [January 3, 2022, 2:46pm UTC](https://discourse.gohugo.io/t/get-resource-from-page-bundle-or-the-assets-dir/36340/1 "2022-01-03T14:46:22Z")

</div>

Hi everybody,

Thanks for the improvements all over Hugo in the last time!

I am currently trying to include images automatically from either a page bundle or the assets directory. Because some images might be needed later on many pages and not only on a single one or the other way around.

For that I wrote this partial, that is called in my render-image hook:

```auto
{{ $r := "" }}
{{ with .Page.Resources.GetMatch .Destination }}
    {{ $r = . }}
{{ else }}
    {{ with resources.Get .Destination }}
        {{ $r = . }}
    {{ else }}
        {{ warnf "There is no '%s' in the page resources." .Destination }}
    {{ end }}
{{ end }}
{{ return $r }}

```

This is working for images in page bundles, but not for images in assets. I get always an error like

```auto
There is no '../../../../assets/image.jpeg' in the page resources

```

And I can’t get my head around this, because the image is obviously there. Otherwise the warning could not include it.

Thanks,  
Georg

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [January 3, 2022, 3:36pm UTC](https://discourse.gohugo.io/t/get-resource-from-page-bundle-or-the-assets-dir/36340/2 "2022-01-03T15:36:29Z")

</div>

> [@Georg](#):
>
> And I can’t get my head around this, because the image is obviously there. Otherwise the warning could not include it.

So, `.Destination` is the image path from the markdown file. There is no guarantee that that file will be found in /assets.

Also, your warning message is a little off, as you’re looking in /assets and not in “page resources” in the latter case.

---

<div class="post-metadata">

**Author:** ![alexandros](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@alexandros](https://discourse.gohugo.io/u/alexandros)\
**Post date:** [January 3, 2022, 3:43pm UTC](https://discourse.gohugo.io/t/get-resource-from-page-bundle-or-the-assets-dir/36340/3 "2022-01-03T15:43:25Z")

</div>

I would try something else.

Check whether there is a Page bundle with Page Resources otherwise look in assets.

```auto
{{ $r := "" }}
{{ if .BundleType }}
{{ $r = .Page.Resources.GetMatch .Destination }}
{{ $r = $r.RelPermalink }}
{{ else }}
{{ $r = resources.Get .Destination }}
{{ $r = $r.RelPermalink }}
{{ end }}

```

Also note that the `.BundleType` method returns `leaf` , `branch` , or an empty string.

---

<div class="post-metadata">

**Author:** ![Georg](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/georg/32/21486_2.png) [@Georg](https://discourse.gohugo.io/u/Georg)\
**Post date:** [January 3, 2022, 6:50pm UTC](https://discourse.gohugo.io/t/get-resource-from-page-bundle-or-the-assets-dir/36340/4 "2022-01-03T18:50:11Z")

</div>

Thanks for your help! You’re right about the message, I’ll change that. I’m confused about the .Destination. The image name has been given in the Markdown, right. But there is no relative path included. I think I’m going to create an example repository for the problem as soon as possible.

---

<div class="post-metadata">

**Author:** ![Georg](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/georg/32/21486_2.png) [@Georg](https://discourse.gohugo.io/u/Georg)\
**Post date:** [January 3, 2022, 8:30pm UTC](https://discourse.gohugo.io/t/get-resource-from-page-bundle-or-the-assets-dir/36340/5 "2022-01-03T20:30:10Z")

</div>

You can find an example for my problem on  
[https://github.com/bowman2001/hugo-forum-topic-36340](https://github.com/bowman2001/hugo-forum-topic-36340)

When the two images are inside the page bundle everything is fine.

When you move one of them to the assets folder, the `.Destination` contains the URL to the image with a relative path. But `resources.Get` doesn’t get it. What am I missing?

Thanks, Georg

---

<div class="post-metadata">

**Author:** ![Georg](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/georg/32/21486_2.png) [@Georg](https://discourse.gohugo.io/u/Georg)\
**Post date:** [January 3, 2022, 9:44pm UTC](https://discourse.gohugo.io/t/get-resource-from-page-bundle-or-the-assets-dir/36340/6 "2022-01-03T21:44:59Z")

</div>

Thanks for your hint to `.BundleType`. I didn’t know about it and will keep it in mind. For my current problem it doesn’t provide the solution, because I would like to include images from the asset dir in all kinds of pages.

---

<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:** [January 3, 2022, 9:50pm UTC](https://discourse.gohugo.io/t/get-resource-from-page-bundle-or-the-assets-dir/36340/7 "2022-01-03T21:50:44Z")

</div>

Here’s what I did:

```bash
git clone --recurse-submodules https://github.com/bowman2001/hugo-forum-topic-36340
cd hugo-forum-topic-36340/
mkdir assets
mv content/post/test/kitten.jpeg assets/
hugo server

```

When I visit `http://localhost:1313/post/test/` both images are visible. What does your `assets` directory structure look like? Are you placing the image in the root of `assets` or in a subdirectory?

---

<div class="post-metadata">

**Author:** ![Georg](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/georg/32/21486_2.png) [@Georg](https://discourse.gohugo.io/u/Georg)\
**Post date:** [January 3, 2022, 10:03pm UTC](https://discourse.gohugo.io/t/get-resource-from-page-bundle-or-the-assets-dir/36340/8 "2022-01-03T22:03:53Z")

</div>

Hi,

directly in the root of `assets` — as you did. Did you restart `hugo server`?

I’m using a locally compiled version of hugo: `hugo v0.91.2+extended linux/amd64 BuildDate=unknown`

Thanks for cloning!

---

<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:** [January 3, 2022, 10:04pm UTC](https://discourse.gohugo.io/t/get-resource-from-page-bundle-or-the-assets-dir/36340/9 "2022-01-03T22:04:38Z")

</div>

I did exactly as I posted above.

---

<div class="post-metadata">

**Author:** ![Georg](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/georg/32/21486_2.png) [@Georg](https://discourse.gohugo.io/u/Georg)\
**Post date:** [January 3, 2022, 10:08pm UTC](https://discourse.gohugo.io/t/get-resource-from-page-bundle-or-the-assets-dir/36340/10 "2022-01-03T22:08:12Z")

</div>

Yes, thanks. I am going to figure it out tomorrow. I still get `WARN 2022/01/03 22:59:10 There is no '../../../assets/kitten.jpeg' in the local resources.`

---

<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:** [January 3, 2022, 10:10pm UTC](https://discourse.gohugo.io/t/get-resource-from-page-bundle-or-the-assets-dir/36340/11 "2022-01-03T22:10:36Z")

</div>

Do what I did (clone) and see if you get the same results. I suspect there is something different about the repo you posted and the project you are testing with.

---

<div class="post-metadata">

**Author:** ![alexandros](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@alexandros](https://discourse.gohugo.io/u/alexandros)\
**Post date:** [January 4, 2022, 7:04am UTC](https://discourse.gohugo.io/t/get-resource-from-page-bundle-or-the-assets-dir/36340/12 "2022-01-04T07:04:35Z")

</div>

I also tested your repo and I cannot reproduce your issue.

When I move `murray.png` under `/assets/` the image is rendered with your existing code.

Something else is going on as @jmooring has indicated.

---

<div class="post-metadata">

**Author:** ![Georg](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/georg/32/21486_2.png) [@Georg](https://discourse.gohugo.io/u/Georg)\
**Post date:** [January 4, 2022, 8:30am UTC](https://discourse.gohugo.io/t/get-resource-from-page-bundle-or-the-assets-dir/36340/13 "2022-01-04T08:30:37Z")

</div>

Fresh start in the morning: I deleted the repo — which I pushed yesterday — with `rm -rf`. Then repeated your steps. And now it works as it was supposed to and I can move the images back and forth from assets to the bundle. The only difference to what I was doing then, which I’m aware of, is the flag `--recurse-submodules`. There are none in the repo, so I repeated again without the flag: still working. I don’t know, what went wrong yesterday. Kinda weird and funny.

Thank you very much,  
Georg

---

<div class="post-metadata">

**Author:** ![Georg](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/georg/32/21486_2.png) [@Georg](https://discourse.gohugo.io/u/Georg)\
**Post date:** [January 4, 2022, 9:01am UTC](https://discourse.gohugo.io/t/get-resource-from-page-bundle-or-the-assets-dir/36340/14 "2022-01-04T09:01:37Z")

</div>

For my actual project I’m working in Webstorm and moved an image there via drag&drop — same problem again. Then I repeated the step on a terminal — no problem at all. And this is what I was doing yesterday with the example repo. I don’t understand the problem, but the _IDE_ seems to access the file system differently. _Hugo_ had nothing to do with 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:** [January 4, 2022, 11:57am UTC](https://discourse.gohugo.io/t/get-resource-from-page-bundle-or-the-assets-dir/36340/15 "2022-01-04T11:57:04Z")

</div>

Webstorm (IntelliJ) creates a file, then puts the content in, then removes the old file. It’s called something like “safe copy and move” and _should_ be disable-able somewhere in the settings. I am pretty sure IntelliJ also works with file locks - at least from my experience with PhpStorm which is Webstorm with some PHP tools. Depending on the size of the project IntelliJ can be slowed down, which is why I switched to VSCode now.

This all in combination might irritate Hugo or be overlooked when it happens in a fast sequence. Depending on your underlying system you could write a script that on CTRL-C restarts itself again. In a Ubuntu bash shell the following command somewhere at the start of a script will be executed when somebody uses CTRL-C:

```auto
trap "{ echo 'Terminated with Ctrl+C'; }" SIGINT

```

(trap is part of bash and SIGINT is CTRL+C. do `trap --help` to learn about other signals that can be “trapped”)

Everything from `echo` to `;` will be executed and could be (ab)used to load start the script again. I am using that to restart with CTRL+C - disadvantage, you have to terminate the shell to kill hugo and sometimes that is dirty and Hugo perseveres and needs to be killed with `kill -9 hugo`.

---

<div class="post-metadata">

**Author:** ![Georg](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/georg/32/21486_2.png) [@Georg](https://discourse.gohugo.io/u/Georg)\
**Post date:** [January 4, 2022, 12:39pm UTC](https://discourse.gohugo.io/t/get-resource-from-page-bundle-or-the-assets-dir/36340/16 "2022-01-04T12:39:47Z")

</div>

Thanks for the insights. I don’t have to move files very often, so I will turn to the terminal for now, if such problems reappear.

---

<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:** [January 6, 2022, 12:39pm UTC](https://discourse.gohugo.io/t/get-resource-from-page-bundle-or-the-assets-dir/36340/17 "2022-01-06T12:39:54Z")

</div>

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