Hyperlink to a file in a sidebar widget

Dear all,
I have added a widget to a sidebar. I try to figure out how to add a hyperlink in this widget to a file related to the current displayed post. The file in question stands in the directory of the post, i.e.
The code of the widget I created is:

{{- $download := .Site.Taxonomies.download }}
<div class="sidecard">
  <div class="widget widget-download block">
    <h3 class="widget-download__title widget__title">{{ T "widget-download_title" }}</h3>
    <div class="widget-download__content widget__content">
      <ul class="widget-download__list widget__list">
        <li class="widget-download__item widget__item">
          <a class="widget-download__link widget__link transition" href="{{ .RelPermalink }}">
            {{- .Title -}}

What is displayed is a link to the current page, but not to the file.pdf
I may change {{- .Title -}} but I don’t know what syntax I should use instead.
Thank you for your help.


├── posts/
│   └── my-post/
│       ├── index.md
│       └── my-file.pdf
└── _index.md


{{ with .Resources.Get "my-file.pdf" }}
  <a href="{{ .RelPermalink }}">{{ .Name }}</a>
{{ end }}

If you want to force the browser to download the file instead of opening it, add the download attribute to the anchor element.

Thank you very much for your swift reply. I was just logging in to reply to myself as I found a solution which may not be as elegant as yours. I do not wish to hard-code the name of the file in the widgets file. So I wrote this code which makes it:

{{ with .File }}
  <a class="widget-download__link widget__link transition" href="../{{ .TranslationBaseName }}.pdf" target="_blank">
  {{- .File -}}
{{ end }}

Unless there is a way to avoid hard-coding the name of the file as this name changes for every post.

Many thanks again.

{{ with .Resources.GetMatch "*.pdf" }}
  <a href="{{ .RelPermalink }}">{{ .Name }}</a>
{{ end }}

This will return the first file that matches the glob pattern.

Or use the printf function in conjunction with the .Resources.Get method.

This is great! Thank you very much.

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