Make relative link to directory

I’d like to supply downloads to a blog post. I would like to place the files in the same folder as the blog post.

The directory struture generated from hugo is the following:

public/2016/06/22/
├── myasset.xml
└── mypost
    └── index.html

How can I link from my post to the file myasset.xml without giving the full URL such as [download](/2016/06/22/myasset.xml) - which would be bad if I decide to move the post.

You could make the path relative to the current directory (/). Then your link would be [download](/myasset.xml) and can be moved without problems.

I don’t think that would work, @digitalcraftsman.

You could, however, construct your DL URLs using {{ .Dir }} (on Page).

I have tried to use {{ .Dir }} on my .md page, but this is not replaced by anything. The output contains the characters {{ .Dir }}. Perhaps I have done something wrong?

Thanks

Patrick

That is template syntax, it doesn’t work directly in content files. If you’re gonna use it outside of the template files, you’ll need to wrap it in a shortcode.

Note that in shortcode it will be {{ .Page.Dir }}

Thank you, bep. This works now. In my content file I have this:

[my asset](/{{<thisdirectory>}}/myasset.xml) and the following shortcode at thisdirectory.html:

{{ .Page.Dir }}

Thanks again!

Patrick

… even better now:

this is my content file:

{{<download "myasset.xml" >}}

and this is the shortcode download.html file:

<a href="/{{ .Page.Dir }}{{ index .Params 0 }}" download="{{ index .Params 0}}">{{ index .Params 0}}</a>

Which is exactly what I need. Thank you for your help!

Patrick

1 Like