StaPy

Static Site Generator

How-to: How to use dynamic data?

How to use dynamic data?

Step 1

Add a new plugin file in the plugins directory. A plugin is a Python script.

plugins
+--- data.py

Step 2

We want to add a new dynamic variable. The event is page_data_merged. This event is used after merging layouts and page data.

In this example, we add the current year to be added in the copyright.

from datetime import datetime


def page_data_merged(data: dict, args: dict) -> dict:
    data['year'] = datetime.now().year
    return data

In the footer block template, we now add the variable:

<p>Copyright @ {{ year }} Company</p>

Arguments

In the args argument, we find the current page path:

def page_data_merged(data: dict, args: dict) -> dict:
    data['welcome'] = ''
    if args['path'] == '/index.html':
        data['welcome'] = 'Welcome to the homepage!'
    return data

The data argument contains the merged json data for the page:

def page_data_merged(data: dict, args: dict) -> dict:
    if 'meta_title' in data:
        data['meta_title'] = 'Prefix - ' + data['meta_title']
    return data

Debug

To visualize final page data, add the _page key before the page path in the URL:

http://localhost:1985/_page/index.html

Next

How to add a new block?
How to add a new blog post?
How to display a list of links?
How to use dynamic data?
How to use Stapy on Windows?
How to host my static website for free?