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"],
		  			["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
                <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%}

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/..."], 
                        ["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.