Showing Now Playing with Hugo
6th of April, 2022
So I wanted to add "Now playing" footer to my posts so I can share easily the
music that I'm listening currently and maybe some people are able to find
something new and interesting with that so I implemented a very quick and poor
man's implementation for it! Basically I only play with the YouTube's
search_query
URL parameter and I pass in the song to that from the Hugo
post's front matter.
I pass in the current song as a slice in the post's front matter similarly to this:
--- nowPlaying: ["DAF", "Liebe auf den Erste Blick"] ---
Then I just parse that in the template:
{{ if .Params.nowPlaying }} {{ $artist := index .Params.nowPlaying 0 }} {{ $song := index .Params.nowPlaying 1 }} {{ $query := querify "search_query" ( printf "%s %s" $artist $song ) "search_type" "videos" }} <div class="now-playing"> <p class="no-margin">Now playing: <a href="https://www.youtube.com/results?{{ $query | safeURL }}" target="_blank"> {{ $artist }} - {{ $song }} </a> </p> </div> {{ end }}
If you have any questions or suggestions, write to topi at topikettunen dot com.
Now playing: DAF - Liebe auf den Erste Blick