.getPage

I would like to bring to your attention a (to me) inconsistent behaviour:

fileExists will be true whether or not index.md is given but .SiteGetPage only works if index.md is added. This really threw me as I first tested whether the file existed - it did - but then it did not print the title.

{{- $url := printf “%s%s” “/states/rajasthan/cities/jaipur/excursions/amber-fort-sightseeing/” “index.md” }}
{{- fileExists $url }}
{{ with .Site.GetPage $url }}{{ .Title }}{{ end }}

/states/rajasthan/cities/jaipur/excursions/amber-fort-sightseeing/index.md
true
Amber Fort Sightseeing

v0.75.1

1 Like

Do you have a repo demonstrating this? I find the behaviour of fileExists and .GetPage to be predictable.

Yes the repo is https://github.com/ourmaninindia/odyssey-tours.git
The URL for the test page is https://develop--odyssey-tours.netlify.app/test/

The test page itself can be found in themes/odyssey/layouts/test/single.html which simple is as follows:

Without index.md

{{- $url := "/states/rajasthan/cities/jaipur/excursions/amber-fort-sightseeing/" }} fileExists:   {{- fileExists $url }}
Title using GetPage:   {{ with .Site.GetPage $url }}{{ .Title }}{{ end }}

{{- $urlmd := printf “%s%s” $url “index.md” }}

with index.md

fileExists:   {{- fileExists $urlmd }}
Title using GetPage:   {{ with .Site.GetPage $urlmd }}{{ .Title }}{{ end }}

this gives:

Without index.md

fileExists: true
Title using GetPage:

with index.md

fileExists: true
Title using GetPage: Amber Fort Sightseeing

Not necessarily. .Site.GetPage also works for Section and Nested Section lists with an _index.md.

Not sure why you’re performing the following check in your templates:

<h2>Without index.md</h2>
{{- $url := "/states/rajasthan/cities/jaipur/excursions/amber-fort-sightseeing/" }}
fileExists: &nbsp; {{- fileExists $url }}<br>
Title using GetPage: &nbsp; {{ with .Site.GetPage $url }}{{ .Title }}{{ end }}<br>

{{- $urlmd := printf "%s%s" $url "index.md" }}

<h2>with index.md</h2>
fileExists: &nbsp; {{- fileExists $urlmd }}<br>
Title using GetPage: &nbsp; {{ with .Site.GetPage $urlmd }}{{ .Title }}{{ end }}<br>

{{ end }}

In the first instance you are simply checking for the PATH without index.md
/states/rajasthan/cities/jaipur/excursions/amber-fort-sightseeing/
And yes the file exists, as in the rendered Page exists.

fileExists

Checks whether a file exists under the given path.

Then in the second instance you add index.md in the mix and fileExists again reports that an index.md is found.

However in the second case, if you were to rename index.md to _index.md then in that case fileExists would report that the file does not exist.

What are you trying to do?

I am not sure I understand what you are trying to do either.

If you are starting with this $url:

{{- $url := "states/rajasthan/cities/jaipur/excursions/amber-fort-sightseeing/" }}

This is how you .GetPage:

{{ .Site.GetPage (print ( strings.TrimSuffix "/" $url )) }}

See doc: https://gohugo.io/functions/getpage/


This is how you check if index.md exists:

{{- fileExists (print "/content/english" $url "index.md") }}

Thanks guys,

I was merely trying to point out the imho inconsistencies between the two commands i.e. GetPage and fileExists.

I now understand that my problem was that I did not trim the trailing / in the .GetPage command. This is not clear in the docs. At least I had read it several times and it did not occur to me to verify this.

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