How to run conditional logic only if property exists in front matter?

Hello,
I would like to execute the following conditional only if the #+lastmod property (I’m using Org mode for markup) is present in the post file.

#+title: Test Post
#+author: Zoltan 
#+date: 2024-09-15T01:05:18+02:00
#+lastmod: 2024-09-22T01:05:18+02:00

If I remove the #+lastmod the conditional still executes.

{{ with .Lastmod }}
<div class="post-lastmod">
  <time datetime="{{ . | dateFormat "2006-01-02" }}">Updated on {{ . | dateFormat "January 2, 2006" }}</time>
</div>
{{ end }}

By default, if not specified in front matter, the Lastmod method will fall back to modified, then date, then publishdate, then pubdate, then published.

You can change this in your site configuration:
https://gohugo.io/getting-started/configuration/#configure-dates

In that case, I think it would be better to create a custom property, something like updated, to have that control, when the date is specified, print the div and when not, don’t print anything.

Or add this to your site configuration:

[frontmatter]
lastmod = ['lastmod']  # no fallback values
2 Likes

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