Tip: How To Keep Data That Changes Out Of Templates

We have a lot of social media links and things change all the time. We don’t want to pay people to change templates to account for this so we try to keep dynamic data in config files and here is an example of how we do this:

in our config.toml

# Podcast Feed Links
podcastchannels = [ ["iTunes","https://itunes.apple.com/gb/podcast/learn-english-through-listening/id1134891957"],
					["Spotify","https://open.spotify.com/show/7ixeOS7ezPTZSaISIx2TTw"],
					["TuneIn","https://tunein.com/podcasts/Educational/Learn-English-Through-Listening-p888646/"], 
		  			["Stitcher","https://www.stitcher.com/podcast/adept-english-podcast-series/learn-english-through-listening-podcast-series?refid=stpr"],
		  			["BluBrry","https://www.blubrry.com/adeptenglish/"], 
		  			["PodBean","https://www.podbean.com/podcast-detail/qpp5u-436c6/Learn+English+Through+Listening"], 
		  			["Google","https://play.google.com/music/m/Ick5cnoda663a262rrv6cdpjtnm"], 
		  			["PlayerFM","https://player.fm/series/series-1511313"] ]

Then in our footer template we use this information like this:

<div class="column is-one-half-mobile">
                <h3 class="is-size-6 has-text-white-ter">
                🎧 listen to us
                </h3>
                <div class="columns is-mobile is-multiline">
                  {% if config.extra.podcastchannels %}
                    {% for channel in config.extra.podcastchannels %}
                      <a href="{{channel | last}}" target="_blank"><div class="column has-text-white-ter">🎙️{{ channel | first }}</div></a>
                    {% endfor %}
                  {% endif%}
                  <br>
                </div>
              </div>

We are using Bulma for our styles so excuse the classes if you don’t.

1 Like

Alternatively for data you don’t need to be loaded on every single page via config, you could also store these in a separate file within /content/ and then import them as data for a specific template. For example- using your code- if you place all this in /content/podcasts.toml

    # Podcast Feed Links 
    podcastchannels = [ ["iTunes","https://itunes.apple.com/..."], 
                        ["Spotify","https://open.spotify.com/show/..."], 
                        ["TuneIn","https://tunein.com/podcasts..."], ]
                      

you can then bring that into a single template:

{% set podcasts = load_data(path="content/podcasts.toml") %}

{% if podcasts.podcastchannels %}
    {% for channel in podcasts.podcastchannels %}
        <a href="{{channel | last}}" target="_blank">{{ channel | first }}</a>
     {% endfor %}
 {% endif%}


Depends on use case but your example is nice because there’s only one file to edit!

I’m so happy you pointed this out. I have a specific set of data items (mostly mp3 id tags that I need to include on pages with podcasts). I’d never thought about using this load_data function. Now I have a little python utility that generates valid toml having parsed the mp3 files and it creates the toml file which I now just import. So no need to hand craft the data anymore.

The only issue is I can’t catch errors if the data file does not exist. So the build stops.

Thanks for your suggestion.