Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Support

Date header does not seem to be taken into consideration

Solved by pamtbaau View solution

Started by squeak 2 years ago · 6 replies · 268 views
2 years ago

I'm trying to set a post date in a site using casper theme.
In the casper theme code, I can see that the date is parsed using:

TWIG
{{ page.date|date('F d. Y.') }}

So I assumed that setting the header of my post as the following would display the appropriate date in the post page. Instead, it's displaying today's date. So it seems it's taking as date when I last modified the post's markdown file.

MD
----
template: item
title: Some blogpost
date: 2022-01-06
---

Please note that I have the appropriate date format in my config/system.yaml file:

YAML
  dateformat:
    default: Y-m-d
    short: Y-m-d
    long: Y-m-d_H:i

Anyone has an idea why the date is not displaying as it should?

In a custom theme, I used the following twig expression and it's behaving as expected:

TWIG
{{ page.header.date|date(system.pages.dateformat.short) }}

I would really like to avoid having to customize the casper theme to achieve this behaviour which seem to me like it would be the expected one.

Or did I misunderstand something?

2 years ago

@squeak, According the documentation about the date frontmatter it says:

Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/ ), then the American m/d/y is assumed; whereas if the separator is a dash (- ) or a dot (. ), then the European d.m.y format is assumed.

Try one of the supported formats. Eg.:

YAML
---
template: item
title: Some blogpost
date: 06-01-2022
---
2 years ago

Hello @pamtbaau, thanks a lot for your answer.
Indeed trying the format you propose does work. I'll consider using this workaround if there's no alternative.

However I would expect that filling the system config dateformat the way I did, would allow me to use the date format it describes. It's a bit confusing to me to be able to specify a default date format, but that the date I input is not used if I don't use the standard grav date format.

It's even stranger because the Y-m-d formatted date is recognized (which is not the case if I don't set the dateformat.default config, in which case an error is thrown). It is recognized, but not used.
I'm a bit perplex with this issue. I think I'll dig more into grav issue tracker and maybe post an issue there.

👍 1
2 years ago

After a bit more investigations.
The following header does work:

YAML
---
template: item
title: Some blogpost
date: "2022-01-06"
---

Note the quotes around the date.
All this is strange ;)

2 years ago Solution

@squeak, When using the following format in system.yaml:

YAML
pages:
  dateformat:
    default: U

And the following date in the header of the page:

TXT
date: 2024-01-01   # without quotes

Will output January 01. 2024. using {{ page.date|date('F d. Y.') }}

Note:

  • When using a date without quotes, the date is seen as unixepoch internally. Hence the U as format.
👍 2
2 years ago

Thanks a lot @pamtbaau for posting about this solution :)

I couln't test it now with casper because I stopped using it and decided to build a custom theme. In my custom theme, I use:

TXT
post.header.date|date(system.pages.dateformat.short)

in the template. And set config/system.yaml to:

YAML
  dateformat:
    default: Y-m-d

Then in pages I set dates with the following format:

TXT
date: 2022-01-06

It all works as I'd expect it to. And outputs 2022-01-06 in pages.

Suggested topics

Topic Participants Replies Views Activity
Support · by Thomas, 1 week ago
2 56 13 hours ago
Support · by Anna, 3 days ago
2 61 16 hours ago
Support · by Justin Young, 17 hours ago
1 32 17 hours ago
Support · by Duc , 1 week ago
2 66 5 days ago
Support · by Colin Hume, 1 week ago
2 59 5 days ago