Getting the URL to a page resource

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

Within, 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:

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/":
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:

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

I get:


 -O docker-compose.yml

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

Rename to

└── post/
    └── setup-guide/
        ├── 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.