Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tpl: Add os.fileExists template function #3853

Merged
merged 3 commits into from
Sep 28, 2017

Conversation

digitalcraftsman
Copy link
Member

This pull request is intended to fix #3839

Currently, I've an issue with the test cases. fileExists returns a boolean and an error / nil.

Due to the type assertion false can become ambiguous: it can either mean that a file does not exist or that an error is expected.

If the file does not exist, fileExists returns false (correctly), implicating that no error occurred, but the test fails because false is interpreted like an expected error.

Any ideas how this ambiguity can be circumvented?

@bep
Copy link
Member

bep commented Sep 1, 2017

Any ideas how this ambiguity can be circumvented?

Check the error returned from Stat.

if !os.IsNotExist(err) {
   return err
}

return false

@bep
Copy link
Member

bep commented Sep 28, 2017

@moorereason @digitalcraftsman status?

@bep
Copy link
Member

bep commented Sep 28, 2017

In general, it would be great if you could check if your PR goes green before you leave it to dry.

@bep bep merged commit 2818878 into gohugoio:master Sep 28, 2017
@digitalcraftsman digitalcraftsman deleted the fileExists branch September 29, 2017 10:53
@NicoHood
Copy link

This function seems to only work with absolute paths. Could some please help me (add this to the docs as well!) to use a relative path (checking if an image inside the posts folder exists), or update the function to support both?

I used this dirty workaround:

        {{ $imgPath := print "/content/" $.Dir "header.png" }}
        {{ if (fileExists $imgPath) -}}
              <img alt="{{ .Title }}" class="img-fluid mb-2" src="header.png" />
        {{ end }}

@moorereason
Copy link
Contributor

@NicoHood, please don't comment on issues that were closed 3 years ago. Open a new ticket or ask on the discussion forums.

@gohugoio gohugoio locked as resolved and limited conversation to collaborators Aug 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add template func to check if a file exists
4 participants