Getting the URL to a page resource

content
└── post
    ├── setup-guide
    │   ├── sample_config.yml
    │   ├── app.conf
    │   └── docker-compose.yml
    └── setup-guide.md

Within setup-guide.md, I’d like to be able to get the absolute URL of some of the supporting files so I can provide sample download code like this:

```sh
wget {{< ref "app.conf" >}} -O app.conf
```

But ref isn’t suitable for this task because app.conf isn’t a page. I imagine the answer is simple but I haven’t been able to find a way to do this yet. How do I get the absolute URL of a page resource?

That is part of the story.

That said, there isn’t a built-in shortcode to get the reference to a resource, but it’s fairly simple to create one, e.g:

{{ $resource := .Page.Resources.Get ($.Get 0) }}
{{ with $resource }}
{{ $resource.Permalink }}
{{ end }}

The above is typed from memory, but probably correct …

Error: Error building site: ".../content/post/setup-guilde.md:49:6":
failed to render shortcode "resource-ref": failed to process shortcode:
".../layouts/shortcodes/resource-ref.html:1:21": execute of template failed:
template: shortcodes/resource-ref.html:1:21: executing
"shortcodes/resource-ref.html" at <.Page.Resources.Get>: can't evaluate
field Get in type resource.Resources

OK, my memory is fading … Try

{{ $resource := .Page.Resources.GetMatch ($.Get 0) }}
{{ with $resource }}
{{ $resource.Permalink }}
{{ end }}

With that change and the following in my content file:

```sh
wget {{< resource-ref "docker-compose.yml" >}} -O docker-compose.yml
```

I get:

wget 

 -O docker-compose.yml

Obviously it’s getting a blank string now. Any idea what’s wrong here?

Rename setup-guide.md to index.md

content/
└── post/
    └── setup-guide/
        ├── index.md
        ├── app.conf
        ├── docker-compose.yml
        └── sample_config.yml

Page resources only exist with bundles, either leaf or branch.

1 Like

Oh! Thank you so much! You’re right.

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