Skip to content

Add partial with created and updated dates to page template #9

Closed

Description

@JeremyMcCormick

For instance, this will show the created and updated dates using information from git:

{{ $creationDate := now }}
{{ $updatedDate := now }}
{{ if .GitInfo }}
  {{ $creationDate = .GitInfo.AuthorDate }}
  {{ $updatedDate = .GitInfo.CommitterDate }}
{{ end }}

<div class="date">
  <p>Created: {{- $creationDate.Format "January 02, 2006" -}}</p>
  {{ if ne $creationDate.Unix $updatedDate.Unix }}
    <p>Updated: {{- $updatedDate.Format "January 02, 2006" -}}</p>
  {{ end }}
</div>

There is a partial page-dates.html but this might work better as it is automatic.

Activity

  1. gmantele commented on Mar 21, 2024

    @gmantele
    Member

    I might have forgotten to mention that in the page-dates.html I proposed through #8 , I make the Updated date visible only if different from the Created date. Because of some weird coincidence, I sometimes have few minutes difference between both dates though the document has been created/updated in the same time. That's why I compare the two dates with a delta of 5 minutes. If this is not desired here, ignore this delta (as Jeremy did in the example above).

  2. gmantele commented on Mar 21, 2024

    @gmantele
    Member

    @JeremyMcCormick , in your example, you explicitly use .GitInfo dates. I have also found useful to use .Date and .LastMod as they can have a value depending on which information is available. That is configured in hugo.toml (or hugo.yaml).

    In my case, I configured they as follows:

    enableGitInfo = true
    
    [frontmatter]
      date        = ['date', 'lastmod', ':fileModTime']
      lastmod     = ['lastmod', ':git', ':fileModTime', 'date', 'publishDate']

    It means the following:

    • .Date is set (by priority order) with:
      • the date given in the FrontMatter (header of the Markdown file)
      • the lastmod given in the FrontMatter
      • the filesystem last modification date
    • .LastMod is set (still by priority order) with:
      • the lastmod given in the FrontMatter
      • the date of the last Git commit that included this file
      • the filesystem last modification date
      • the date given in the FrontMatter (probably not necessary)
      • the publishDate given in the FrontMatter (probably not necessary as it is usually used to publish a page in the future)

    By the way, are you sure about .GitInfo.CommitterDate? It seems to rather be .GitInfo.CommitDate (see Hugo GitInfo).

    For more readability, I also recommend using this syntax (with instead of if), though it is really a personal preference and not a recommandation (using if and prefixing properties to get with .GitInfo also works):

    {{ with .GitInfo }}
      {{ $creationDate = .AuthorDate }}
      {{ $updatedDate = .CommitDate }}
    {{ end }}
    
  3. gmantele commented on Mar 21, 2024

    @gmantele
    Member

    Sorry about the mistake in partials/page-dates.html. I've just fixed it and committed directly in main branch.
    I also inserted this partial in all single pages. Feel free to remove this line if not desired.

    To get the GitInfo through this partial, one should add enableGitInfo = true in hugo.toml or add the appropriate option when running Hugo (server or not).

    I also noticed that this partial display date and time. In your example, you display only the date. It is probably better as there is no need to deal with any delta. So, feel free to adapte this partial if you prefer to display only the date.

  4. JeremyMcCormick commented on Mar 21, 2024

    @JeremyMcCormick
    CollaboratorAuthor

    We'll use your partial. I'll close this out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions