fileExists used in my "if" condition never evaluates as "true"

I have a slight problem with fileExists in an if condition.

I’m running the code listed below from _default/single.html template and the image file, for which existence I check against, does exist. In the second line I’m printing out content of $header_img variable, which holds file’s absolute path, and it is seems to be correct, because by using this URL I can open the image in my browser. However, for some reason this condition never evaluates as true.

I checked this code many times for possible typos or other mistakes, but I didn’t catch anything suspicious. Could you please take a look at it and see what’s wrong with it?

{{- $header_img := (path.Join .Permalink "header-image.jpg") -}}
{{ $header_img }} <!-- This returns a valid absolute URL of the image file. -->
{{ if (fileExists $header_img) -}}
	<p>true</p>
	<img src="header-image.jpg" class="post-header-image" width="1280" height="400">
{{- end }}

my 2 cents

you should check the source path and not the destination path of the image!

{{ if (fileExists "header-image.jpg" ) -}}

1 Like

Thanks, but I’m not sure how to interpret source/destination paths. Did you perhaps mean relative/absolute?

Unfortunately, with relative path the result is the same. if evaluates as false and neither the text nor the image is displayed.

{{ if (fileExists "header-image.jpg") -}}
	<p>true</p>
	<img src="header-image.jpg" class="post-header-image" width="1280" height="400">
{{- end }}

The path argument is relative to the root of your project, regardless of whether or not it begins with a slash:

{{ fileExists "config.toml" }}  --> true
{{ fileExists "/config.toml" }} --> true

You need to tell fileExist the path to where the files actually resides. Most likely inside the “static” or “assets” directory.

I solved it like this in shortcodes for my Zen theme.

I add “/static” or “/assets” to the file path and then fileExists can do its thing.

P.S. The shortcode is quite complex since it covers many use cases but the point here is only the selected line.

I see. So, from what I understand, if my image file resides in https://hugosite.com/blog/postn/header-image.jpg (/content/blog/postn/header-image.jpg before I build the site), and this is the contents of $header_img variable from my first post, then the following code:

{{- $header_img := (path.Join .Permalink "header-image.jpg") -}}
{{ $header_img }} <!-- This returns a valid absolute URL of the image file. -->
{{ if (fileExists $header_img) -}}
<!-- ... -->
{{ end }}

will never be evaluated as true because it tests against the existence of https://hugosite.com/https://hugosite.com/blog/postn/header-image.jpg file?

Ok, that seemed to be the case. I fixed it with trimming BaseURL from my variable:

{{ if (fileExists (trim $header_img .Site.BaseURL)) -}}

Thanks, guys! It would probably take me a long time to figure this out, if you didn’t put me on the right tracks. :beers:

In your example, $header_img evaluates to https://hugosite.com/blog/postn/header-image.jpg which is not a valid file system path. The fileExists function tests where a file resides on the file system, not the URL from which it is served.

With this structure:

content/
└── blog/
    └── postn/
        ├── header-image.jpg
        └── index.md

It seems like it would be a lot easier to do something like:

{{ with .Resources.GetMatch "header-image.jpg" }}
  <img src="{{ .Permalink }}" width="{{ .Width }}" height="{{ .Height }}">
{{ end }}
1 Like

Damn, you’re right. Suddenly @ju52’s explanation makes perfect sense now. :flushed:
Thanks again, really appreciate it.

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