Why is page params key lowercase?

This is super annoying, so maybe someone can provide me an alternative way to fix this:
Frontmatter Params:

references: { 
	"Encyclopedia of the Great Plains: Rainmaking" : "http://plainshumanities.unl.edu/encyclopedia/doc/egp.wat.023",
	"Kansas Historical Society: Rainmaking" : "http://www.kshs.org/index.php?url=kansapedia/rainmaking/12179", 
	"Rain-Makers: Wizards, Kings…and Uncle Sam" : "http://yesteryearsnews.wordpress.com/2011/06/14/rain-makers-wizards-kings-and-uncle-sam/"
}

Single post code:

{{ range $key, $value := .Params.references }}
<li><a href="{{ $value }}" target="_blank" rel="nofollow">{{ $key }}</a></li>
{{ end }}

This is to replace my old code where I was just including URLs in an array: https://weathermodificationhistory.com/1891-ohio-rain-wizard-frank-melbourne/

This live version was generated with Hugo 0.26 and it works fine, $key kept its capitalization.

As you can see, the value has to preserve case or the URLs won’t work, but I need the title’s of the article references to preserve their case as well. Is there a better way to do this?

I am using Hugo 0.45 and it lowercases the $key. Why the change?

I can’t speak to why the key is lowercased now (I’m sure there was much thought put into the decision).


But, you can easily achieve what you want by organizing your params a bit differently – use an array of objects:

{
  "references" : [
    {
      "title" : "Some Title",
      "url" : "https://example.com"
    },
    {
      "title" : "Some Title",
      "url" : "https://example.com"
    },
    {
      "title" : "Some Title",
      "url" : "https://example.com"
    }
  ]
}

Then in your template:

<ul>
{{ range .Params.references }}
  <li><a href="{{ .url }}" target="_blank" rel="nofollow">{{ .title }}</a></li>
{{ end }}
</ul>
5 Likes

https://github.com/spf13/viper – the library we use for configuration – does this.

The reasons are:

  • So we can do case insensitive lookups, .Param "Abra.Kadabra vs .Param "abra.kadabra
  • So we can merge configurations from different sources with potentially different case semantics (OS env vars are all upper case)

In short, you should not put data in your map keys in front matter or config data. But note that you can also put data files inside /data.

3 Likes

THANKS!

This worked perfectly, although I had to remove some of code:

ERROR 2018/12/14 18:27:12 “D:\Hugo\Sites\weathermodificiationhistory.com\content\timeline\1891-ohio-rain-wizard-frank-melbourne.md:11:1”: failed to unmarshal YAML: yaml: line 10: could not find expected ‘:’

references : [
{
  "title" : "Encyclopedia of the Great Plains: Rainmaking",
  "url" : "http://plainshumanities.unl.edu/encyclopedia/doc/egp.wat.023"
},
{
  "title" : "Kansas Historical Society: Rainmaking",
  "url" : "http://www.kshs.org/index.php?url=kansapedia/rainmaking/12179"
},
{
  "title" : "Rain-Makers: Wizards, Kings…and Uncle Sam",
  "url" : "http://yesteryearsnews.wordpress.com/2011/06/14/rain-makers-wizards-kings-and-uncle-sam/"
}
]

After I removed the outside { & } worked perfectly.

And thank you to bep for the backstory on the whys and what for’s.

I guess it’s because I am using YAML instead of TOML?

My Front Matter:

---
slug: "1891-ohio-rain-wizard-frank-melbourne"
title: "“Ohio Rain Wizard” Frank Melbourne"
type: "timeline"
date: "1891-01-01"
lastmod: "2017-11-20"
event: "1891-1893"
banner: "Ohio-Rain-Wizard-Frank-Melbourne.jpg"
image: [ "rainmaker-newark-daily-advocate-oh-2-jul-1891.jpg","rainmaker-the-new-era-humeston-ia-18-nov-1891.jpg","rainmaker-the-evening-news-lincoln-ne-27-jul-1892.jpg"]
references : [
{
  "title" : "Encyclopedia of the Great Plains: Rainmaking",
  "url" : "http://plainshumanities.unl.edu/encyclopedia/doc/egp.wat.023"
},
{
  "title" : "Kansas Historical Society: Rainmaking",
  "url" : "http://www.kshs.org/index.php?url=kansapedia/rainmaking/12179"
},
{
  "title" : "Rain-Makers: Wizards, Kings…and Uncle Sam",
  "url" : "http://yesteryearsnews.wordpress.com/2011/06/14/rain-makers-wizards-kings-and-uncle-sam/"
}
]
categories: [ "Pluviculture" ]
---

I assumed your front matter was in JSON, so I gave the example in JSON. Now that I see your front matter is in YAML, I was surprised it worked for you. Then I learned that JSON is valid YAML, pretty funny.

If you wanted to rewrite this “the YAML way”, it’d be:

---
references:
  - title: "Some Title"
    url: "https://example.com"
  - title: "Some Title"
    url: "https://example.com"
  - title: "Some Title"
    url: "https://example.com"
---
2 Likes

Thanks zwbetz,
I am a self taught trial by error coder, lol.
I liked YAML because it seemed simpler, and the code you just showed me is simpler.
I have three sites that I have turned into Hugo sites and I absolutely love it!
https://climateviewer.com/ (blog, used to be Wordpress)
http://climateviewer.org/ (map and 3D cesium globe)
and https://weathermodificationhistory.com/ (timeline, history references)
I suggest Hugo to everyone now.
Thanks again for the quick responses. If a newbie like me can use Hugo, everyone should be using it!