• Posted on February 17, 2017

Dynamic Copyright Year when using Hugo

After the New Year I always check the footer on websites to see which sites forgot to update the copyright year. If you have a dynamic site, this shouldn’t be an issue at all since you can easily program your site to show the current year. I’ve been using Hugo for a few months to generate my static websites and wanted an easier way to update the copyright year, or better yet, not have to update the copyright year.

Below is a quick tutorial on how I have the copyright year for my sites that use Hugo update without having to change any configuration files manually. Because Hugo is a static site generator, you will still need to generate and upload the new site each year, but you don’t have to worry about remembering to change the copyright year every year. Whenever you make your first change of the year, the copyright will update along with the modifications.

To set your Hugo site to update the copyright year without having to remember to do it manually each year, use the following steps.

  1. Open the Hugo configuration file (config.toml) and find the line for the copyright text.
  2. Modify the value for copyright to show the placeholder text {year} instead of the actual year.
  3. Open the template file that displays the copyright. This is usually in a file called footer.html. If you are unable to find what file is used to display the copyright text, use findstr or grep to search for the string .Site.Copyright.
  4. Replace the code to display the copyright text, which is usually {{ .Site.Copyright }} with {{ replace .Site.Copyright "{year}" now.Year }}.
baseURL = "http://www.example.com/"
title = "Example Site"
copyright = "Copyright 2007-{year} Example LLC. All rights reserved."
	<span class="copyright">
		{{ replace .Site.Copyright "{year}" now.Year }}

The above code is replacing all instances of the text {year} within the variable .Site.Copyright with the current year. We can get the current year by using the now.Year variable from the now function.

Other Methods

There are other methods to keep the copyright year text consistent with the current year, such as using JavaScript. With JavaScript you would grab the footer text and replace a placeholder with the current year (similar to what we do above). I don’t recommend using JavaScript to modify the copyright year of your site since it can lead to cached versions of your site showing the incorrect year along with showing the wrong year for users who have JavaScript disabled.