Pillar extensions can now register global Jinja2 context processors.

This commit is contained in:
Sybren A. Stüvel 2017-06-14 16:10:11 +02:00
parent 94d12c6b66
commit efa2321ac3
2 changed files with 19 additions and 0 deletions

View File

@ -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')

View File

@ -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 {}