Variable File Link

This isn’t working for me:

The name of the file you want to link can be specified as a variable instead of an actual file name. For example, suppose you defined a variable in your page’s front matter like this:

---
title: My page
my_variable: footer_company_a.html
---

You could then reference that variable in your link:

{% link {{ page.my_variable }} %}

In this example, the link tag would render a link to the file footer_company_a.html.

I’ve tried both declaring a variable in the front matter using a hardcoded path, such as:

icon_path: assets/images/components/clutch.png

and using:

{% assign icon_path = component_page.slug | prepend: assets/images/ | append: ".png" %}
<img src="{% link {{ icon_path }} %}"/>

to do it for a bunch of icons in a loop.

Either way I get an error like this:

Liquid Exception: Could not find document ‘{{ icon_path }}’ in tag ‘link’. Make sure the document exists and the path is correct. in /_layouts/components-index.html
jekyll 3.9.2 | Error: Could not find document ‘{{ icon_path }}’ in tag ‘link’.

Make sure the document exists and the path is correct.

The error message just shows whichever expression I put to the right of the {% link leading me to believe that it’s insistently trying to interpret the argument as a literal path despite the double-braces.

The images show up fine when I don’t use the link tag:

<img src="/assets/images/components/{{ component_page.slug }}.png"/>

but I’d like to have Jekyll detect broken links / missing files.

Has this feature been removed from Jekyll?

Ok, the link tag seems broken to me.

Even a literal argument like {% link _components/clutch.md %} results in the literal URL {% link _components/clutch.md %}.

However, it does work on the home page where I’ve got one link as {% link _components/index.md %} which results in /components

In Jekyll 3, the link tag cannot use variables. Jekyll 3 is what GitHub pages uses, but the jekyllrb.com site documents the latest Jekyll 4. Most new 4 features are marked, but clearly not in this case. The Jekyll 3 and 4 link tags are mostly incompatible.

I haven’t found any good source of Jekyll 3 docs, but here’s the link doc:

Slightly outdated 3 docs: DevDocs
Latest 3 doc source: jekyll/templates.md at 3.9-stable · jekyll/jekyll · GitHub

It is probably best if you just use an href like this:

<a href="{{- page.my_variable | relative_url -}}">Link</a>

If you are using Markdown, you can use:

[Link]({{page.my_variable | relative_url}})