Data Hierarchy

Data for use in templates can come from the following places:

  • Data files in the top-level data folder
  • Data files nested in sub-directories
  • Data in frontmatter

Data from lower down in the list overrides data from higher up. Any nested data file overrides those from parent directories.


your project structure looks like this:


And contains the following files:

# data/global-data.json

{ "country": "United States", "color": "blue"}
# posts/

def get_data():
    return {"color": "red", "language": "English"}
# posts/nested-dir/nested-data.json

{ "language": "Spanish", "shape": "circle" }
<!-- jinja2.j2 -->
shape: rectangle

    <li> {{ }} </li>
    <li> {{ data.color }} </li>
    <li> {{ data.language }} </li>
    <li> {{ data.shape }} </li>

The resulting file would look like:

    <li> United States </li>
    <li> red </li>
    <li> Spanish </li>
    <li> rectangle </li>

Data files that don't return dicts

If you have a data file that doesn't return a dict:

# data/

def get_data():
    return ['Doc', 'Grumpy', 'Happy', 'Sleepy', 'Bashful', 'Sneezy', 'Dopy']

You access it in a template at data.<filename>:

    {% for dwarf in data.dwarfs %}
    <li> {{ dwarf }} </li>
    {% endfor %}