# Creating a link if the corresponding file exists

**URL:** <https://discourse.gohugo.io/t/creating-a-link-if-the-corresponding-file-exists/53683>\
**Category:** support\
**Created:** [February 25, 2025, 9:35am UTC](https://discourse.gohugo.io/t/creating-a-link-if-the-corresponding-file-exists/53683 "2025-02-25T09:35:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zi7loh](https://avatars.discourse-cdn.com/v4/letter/z/8c91f0/32.png) [@zi7loh](https://discourse.gohugo.io/u/zi7loh)\
**Post date:** [February 25, 2025, 9:35am UTC](https://discourse.gohugo.io/t/creating-a-link-if-the-corresponding-file-exists/53683/1 "2025-02-25T09:35:57Z")

</div>

I’m parsing an YAML file with a list of publications, some of which have corresponding PDF files. I’d like to provide a link when a file exists, but not when it doesn’t.

This is what I’m currently doing:

```auto
{{ $publications := sort .Site.Data.publications.references "citation-key" }}
<ul>
	{{ range $publications }}
	<li>
		{{ .title }}
		{{ if fileExists "static/repository/{{ .id }}.pdf" }}<a href="/repository/{{ .id }}.pdf">[PDF]</a>{{ end }}
	</li>
	{{ end }}
	<ul>

```

If I change the `fileExists` by providing one of the file names in the folder, it evaluates true and the corresponding links are correct

So

```auto
{{ if fileExists "static/repository/actualfilename.pdf" }}<a href="/repository/{{ .id }}.pdf">[PDF]</a>{{ end }}

```

Evaluates true and I get links for all of files.

What confuses me is that `.id` is then parsed correctly and I get working links to the right files. Why does `.id` not refer to the filenames correctly when checking `if fileExists`, but does when ranging over the file names for the links?

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [February 25, 2025, 10:08am UTC](https://discourse.gohugo.io/t/creating-a-link-if-the-corresponding-file-exists/53683/2 "2025-02-25T10:08:32Z")

</div>

It would be easier to help you if you properly formatted your code. What you’ve posted, as you have posted it, it hard to understand.

---

<div class="post-metadata">

**Author:** ![zi7loh](https://avatars.discourse-cdn.com/v4/letter/z/8c91f0/32.png) [@zi7loh](https://discourse.gohugo.io/u/zi7loh)\
**Post date:** [February 25, 2025, 10:51am UTC](https://discourse.gohugo.io/t/creating-a-link-if-the-corresponding-file-exists/53683/3 "2025-02-25T10:51:10Z")

</div>

How would you like me to format the code?

I tried to simplify the formatting.

---

<div class="post-metadata">

**Author:** ![zi7loh](https://avatars.discourse-cdn.com/v4/letter/z/8c91f0/32.png) [@zi7loh](https://discourse.gohugo.io/u/zi7loh)\
**Post date:** [February 25, 2025, 1:17pm UTC](https://discourse.gohugo.io/t/creating-a-link-if-the-corresponding-file-exists/53683/4 "2025-02-25T13:17:57Z")

</div>

Is solved the problem with

```auto
{{ $id := .id }}
{{ $path := "/static/repository" }}
{{ if fileExists (print $path "/" $id ".pdf") }}<a href="/repository/{{ .id }}.pdf">[PDF]</a>{{ end }}

```

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [February 27, 2025, 1:18pm UTC](https://discourse.gohugo.io/t/creating-a-link-if-the-corresponding-file-exists/53683/5 "2025-02-27T13:18:52Z")

</div>

Closed on February 27, 2025, 1:18pm UTC

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