Content: is it same for Page and Post? And how to hide date

@askar To expand a little on @moorereason 's response:

I’m assuming that <span class="post-date">{{ .Date.Format "Mon, Jan 2, 2006" }}</span> is from a Hugo theme that you’ve downloaded. Although you may not want to have the date write to the page for your end-user, you’ll eventually be grateful that this little chunk of metadata is included in all of your content files.

You mentioned that writing the date to the page “would be okay if making a blog post,” so I’m assuming you’ll need it for some of your content. If you want to set this at the page/layout level, you can add page/content-specific metadata in the toml/yaml head of your new .md file and then create a conditional statement for displaying the date.

For yaml:

---
title: "My Blog Post"
date: "2015-12-09T15:03:01+09:00"
showthedate: true
---

For TOML:

title = "My Blog Post"
date = "2015-12-09T15:03:01+09:00"
showthedate = true

You can make this easier by deciding if you want a default value for particular sections. So, for example, you may want all posts to show the date (you can also use .PublishDate, which is a built-in page variable with Hugo) by default and create a file in your archetypes folder named post.md that shows that looks like the following:

---
showthedate: true
---

Which will create a file that includes both the title and date by default and then will add your new param each time you create a new post from the command line.

Now you can access this little chunk of metadata using Go templating logic as follows:

{{ if .Params.showthedate }}
<span class="post-date">{{ .Date.Format "Mon, Jan 2, 2006" }}</span>
{{ end }}

If you want to give yourself the opportunity to turn off all dates site-wide by changing a single variable but also leave yourself the granular control on a per-page basis, you could create a site variable in your config file (toml or yaml). You wouldn’t want to do this for dates, but you may have other use cases (eg, wanting to turn off all comments at once for something like Disqus) and it will help give you an idea of the difference between site variables and page variables:

####TOML

#This would be in your config.toml file at your root
[params]
    ShowDatesOnPosts = true

####YAML

#This would bet in your config.yaml file at your root

params:
- ShowDatesOnPosts: true

You could then set this in your templating logic to look for both conditions:

{{ if and (.Params.showthedate) (.Site.Params.ShowDatesOnPosts) }}
<span class="post-date">{{ .Date.Format "Mon, Jan 2, 2006" }}</span>
{{ end }}

Even if you have 10k posts, all you would need to do is change the ShowDatesOnPosts to false in your config.yaml/toml and all dates would be removed from your end-user.

Gotcha: Note that your page-level variables (in this instance, .Params.showthedate) must be set in lowercase in your templating logic.

Hope that helps. Cheers.

5 Likes