I’m trying to use page.dir in my website, but if the current webpage is a post it simply returns empty. If the page is not a post, it functions as described in the documentation. Is there anything I could have configured incorrectly for this to happen? I’ve tried playing around with the permalink setting in _config.yml, but it doesn’t fix it.
More info and a demo on the website I’m currently building: https://marro.dev/blog/page-dir-variable-broken-in-jekyll
(Edit for clarification:)
I tried this code on my website:
The part inbetween square brackets should show the contents of page.dir ([/blog/] in this case): [{{ page.dir }}]
And as a control variable, the part inbetween square brackets should show the contents of page.url ([/blog/page-dir-variable-broken-in-jekyll] in this case): [{{ page.url }}]
The result from this on my website when put into a post (ignore this forum turning two square brackets into a square):
The part inbetween square brackets should show the contents of page.dir ([/blog/] in this case):
And as a control variable, the part inbetween square brackets should show the contents of page.url ([/blog/page-dir-variable-broken-in-jekyll] in this case): [/blog/page-dir-variable-broken-in-jekyll]
Putting similar code into another page which isn’t a post correctly gives:
The part inbetween square brackets should show the contents of page.dir ([/] in this case): [/]
And as a control variable, the part inbetween square brackets should show the contents of page.url ([/portfolio] in this case): [/portfolio]
Oops, that was a mistake in this post that I didn’t make in the website files where I did use page.dir. Copying the same contents to a non-post page on the website makes it work correctly, so I don’t think the problem is a simple typo. I’ll add the testing code to the top post.
I do not believe it is fair to call this a bug, but it is a reasonable feature request!
Per the docs (separating the Note to point out the issue you are encountering):
The path between the source directory and the file of a page, e.g. /pages/ when the page is at path pages/about.md relative to the source directory. This is derived from the url attribute of the page and can therefore be overridden via the permalink key in the front matter. NOTE: This variable is not applicable to posts and documents in user-defined collections. Use the categories variable to get similar info for posts.
Categories come with their issues, so I highly recommend you consider page.url
What are you trying to accomplish? If you define what you want to do on your site and why, perhaps another option is available.
That note in the documentation did not exist when I made this post - it was added in response to my GitHub issue.
I was trying to implement a “back” button which would take you to the parent page, which would the post overview page for all posts of that category. For now the only category would be “blog”, I’d figure out how to generate the category pages when I needed them. But honestly I’d probably not use it, so I’ll just hardcode the back button to go to /blog/.
You could create a page that displays each page or post in a category. You might also want to look up Collections, which can generate a page for each type.