Custom Template Functions

It’s possible to add your own tempate functions which can be used in Jinja templates. You can do it through a custom plugin which registers to jinja_environment_prepared signal.

Any plugin may inject any function to Jinja’s environment, but for our purposes we’ll create a jinja_injector plugin which does only that. To create it, we should create a plugins directory. It is the default directory name for the plugins, but it can be changed by plugins_path option in config.toml. Inside this directory we should create a subdirectory for our plugin with __init__.py file inside, which will mark it as a Python module which Stag will automatically discover and load.

File structure should look like this:

config.toml
plugins/
  jinja_injector/
    __init__.py

__init__.py should have the following contents:

def my_filter(values):
    ...


def my_global_function():
    ...


def update_env(env, site):
    env.filters["my_filter"] = my_filter
    env.globals["my_global_function"] = my_global_function


def register_plugin(site):
    site.signals.jinja_environment_prepared.connect(update_env)

Here, we have created a filter and a global function. They are now available by the names passed to env.filters and env.globals and they are defined in my_filter and my_global_function functions above.