Hi all,
I’m trying to display some content that I’ve got in a TOML file inside my data folder in a specific order . I’m following the basic example here .
My issues.toml
file looks like this:
[Start]
title = "The Start Issue"
published = "1979-05-27"
[Imperfection]
title = "The Imperfection Issue"
published = "1982-05-27"
[Friendship]
title = "The Friendship Issue"
published = "1980-05-27"
And then I’m displaying them in a layout file with
<ul>
{{ range $.Site.Data.issues }}
<li>{{ .title }}</li>
{{ end }}
</ul>
It shows up just fine, but I really want to order my data by published date. Is this possible with an extra argument to the range function? Currently, I can’t quite figure how the data is ordered by default.
Thanks so much,
Sam
bep
March 1, 2017, 5:15pm
2
1 Like
bep
March 1, 2017, 5:18pm
3
But note that for the date sorting to be something other than string sorting, you should consider making your dates into “proper dates”.
As an example, this weird construction from the Hugo docs would not work with strings:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Hugo, a fast and flexible static site generator built with love by spf13, bep, and friends in Go">
<meta name="author" content="Steve Francia (spf13), Bjørn Erik Pedersen (bep), and friends">
<title>Hugo :: A fast and modern static website engine</title>
{{ "<!--icon for browser shortcuts-->" | safeHTML }}
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico">
{{ "<!--icon to represent this web page on Apple iOS-->" | safeHTML }}
<link rel="apple-touch-icon" href="/apple-touch-icon.png">
{{ "<!--Bootstrap core CSS-->" | safeHTML }}
<link rel="stylesheet" type="text/css" href="/css/bootstrap-stripped-gohugo.css">
<link rel="stylesheet" type="text/css" href="/css/bootstrap-changes-gohugo.css">
<link rel="stylesheet" type="text/css" href="/css/bootstrap-additions-gohugo.css">
This file has been truncated. show original
bep
March 1, 2017, 5:21pm
4
Another “also”: The data structure you got in the above, I think, will become a Go map, which is not ordered. (or: the order is undefined).
Look at
[[quotes]]
name = "Execute"
twitter_handle = "@executerun"
quote = "Hah, #gohugo. I was working with #gohugo on #linux but now I realised how easy is to set-up it on #windows. Just need to add binary to #path!"
link = "https://twitter.com/executerun/status/809753145270272005"
date = 2016-12-16T00:00:00Z
[[quotes]]
name = "Janez Čadež"
twitter_handle = "@jamziSLO"
quote = "Building @garazaFRI website in #hugo. This static site generator is soooo damn fast!"
link = "https://twitter.com/jamziSLO/status/817720283977183234"
date = 2017-01-07T00:00:00Z
[[quotes]]
name = "Hans Beck"
twitter_handle = "@EnrichedGamesHB"
quote = "Diving deeper into @GoHugoIO . A lot of docs there, top work! But I've the impressed that #gohugo is far easier than its feels from the docs!"
This file has been truncated. show original
Thanks, Bep! Working like a charm