diff --git a/pillar/__init__.py b/pillar/__init__.py index 668448a5..86f10002 100644 --- a/pillar/__init__.py +++ b/pillar/__init__.py @@ -304,6 +304,14 @@ class PillarServer(Eve): pillar.web.jinja.setup_jinja_env(self.jinja_env) + # Register context processors from extensions + for ext in self.pillar_extensions.values(): + if not ext.has_context_processor: + continue + + self.log.debug('Registering context processor for %s', ext.name) + self.context_processor(ext.context_processor) + def _config_static_dirs(self): # Setup static folder for the instanced app self.static_folder = os.path.join(self.app_root, 'static') diff --git a/pillar/extension.py b/pillar/extension.py index a114ea06..e9310f66 100644 --- a/pillar/extension.py +++ b/pillar/extension.py @@ -26,6 +26,9 @@ class PillarExtension(object, metaclass=abc.ABCMeta): # Set to True when your extension implements the project_settings() method. has_project_settings = False + # Set to True when your extension implements the context_processor() method. + has_context_processor = False + # List of Celery task modules introduced by this extension. celery_task_modules: typing.List[str] = [] @@ -121,3 +124,11 @@ class PillarExtension(object, metaclass=abc.ABCMeta): :param template_args: additional template arguments. :returns: a Flask HTTP response """ + + def context_processor(self) -> dict: + """Returns a dictionary that gets injected into the Flask Jinja2 namespace. + + Set has_context_processor to True when your extension implements this method. + """ + + return {}