Only showing updated_at if it is different from published_at

Is there a way within the HBS files to only show the updated_at value if it differs from published_at?

I currently use something like this.

Published: <time datetime="{{date format="YYYY-MM-DD"}}">{{date published_at}}</time>
{{#if updated_at}}
Updated: <time datetime="{{date format="YYYY-MM-DD"}}">{{date updated_at}}</time>
{{/if}}

It always displays the updated date, since updated_at seems to always contain a value (the published_at value), even if the post was never actually edited/updated.

Is there a way to use if statements or something within handlebars so that it only renders the updated_at time if it’s actually been updated (or different from the published date if it must have a value)? I found this CSS alternative but that’s it…

Related:
https://forum.ghost.org/t/true-updated-at-compare-dates/5656/2

You can try the {{match}} helper. It’s not yet documented, but here’s an example:

{{#match updated_at "!=" published_at}}
...
{{/match}}
3 Likes

Thanks, that works perfectly. :+1:

@vikaspotluri123 Thanks Vikas, this is super helpful!

I suspect that the comparison is running on the full date/time attribute, so it would see Jan 4, 2022 12:00:00 as being different than Jan 4, 2022 14:00:00. Do you know if it’s possible to do this type of check based only on the date and not the time?

You can use the date helper to compare :slightly_smiling_face:

e.g. {{#match (date published_at format="l") "!=" (date updated_at format="l")}}

I haven’t actually tried this, but it should work in theory.

1 Like

Seems to work for me. Thanks! I was actually hoping for functionality like this originally. :sunglasses:

1 Like

@vikaspotluri123 Vikas, you beautiful genius, that worked perfectly. Thank you!

1 Like