Hello Community!
I’m totally a hugo noob, and i have this question:
I want to display just a random quote on the front page.
It would be the easiest (for me), if these are taken out of a csv file.
first of all we have to access the data in your csv file by reading it. Take a look a the getCSV template func. In the next step we need to get a random quote.
Save your csv file in the data folder and change that path in the example below if necessary.
Et voilà:
<!-- load data-->
{{ $quotes := getCSV ";" "data/quotes.csv" }}
<!-- generate random number -->
{{ $random := mod .Now.Second (len $quotes) }}
<!-- output a random quote from your csv file-->
{{ index $quotes $random }}
The upcoming release of Hugo has a new template func called shuffle. This makes our custom “random number generator” obsolete.
Hi, thanks for the code. But I can not get the random thing work on refresh. It got changed only by rebuilding the hugo project. Is it right? And how can I get a random content on refresh?
Hugo is a static page generator. It will indeed only shuffle things on a rebuild.
Doing stuff on refreshing the website requires adding javascript to the mix.