From 4ae36a0dc35b62773bccb7288557622e8964296e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Tue, 6 Sep 2016 18:39:35 +0200 Subject: [PATCH] Allow custom template dirs for extensions --- pillar/__init__.py | 20 +++++++++++++++++++- pillar/extension.py | 9 +++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/pillar/__init__.py b/pillar/__init__.py index c7fb9ebc..24e543da 100644 --- a/pillar/__init__.py +++ b/pillar/__init__.py @@ -37,6 +37,7 @@ class PillarServer(Eve): super(PillarServer, self).__init__(settings=empty_settings, **kwargs) self.pillar_extensions = {} # mapping from extension name to extension object. + self.pillar_extensions_template_paths = [] # list of paths self.app_root = os.path.abspath(app_root) self._load_flask_config() @@ -198,6 +199,16 @@ class PillarServer(Eve): for blueprint in pillar_extension.blueprints(): self.register_blueprint(blueprint, url_prefix=url_prefix) + # Load template paths + tpath = pillar_extension.template_path + if tpath: + self.log.info('Extension %s: adding template path %s', + pillar_extension.name, tpath) + if not os.path.exists(tpath): + raise ValueError('Template path %s for extension %s does not exist.', + tpath, pillar_extension.name) + self.pillar_extensions_template_paths.append(tpath) + # Load extension Eve settings eve_settings = pillar_extension.eve_settings() @@ -212,10 +223,17 @@ class PillarServer(Eve): self.config['DOMAIN'].update(eve_settings['DOMAIN']) def _config_jinja_env(self): + # Start with the extensions... + paths_list = [ + jinja2.FileSystemLoader(path) + for path in reversed(self.pillar_extensions_template_paths) + ] + + # ...then load Pillar paths. pillar_dir = os.path.dirname(os.path.realpath(__file__)) parent_theme_path = os.path.join(pillar_dir, 'web', 'templates') current_path = os.path.join(self.app_root, 'templates') - paths_list = [ + paths_list += [ jinja2.FileSystemLoader(current_path), jinja2.FileSystemLoader(parent_theme_path), self.jinja_loader diff --git a/pillar/extension.py b/pillar/extension.py index 0c7b3e3a..dd6f0f67 100644 --- a/pillar/extension.py +++ b/pillar/extension.py @@ -63,5 +63,14 @@ class PillarExtension(object): :rtype: dict """ + @property + def template_path(self): + """Returns the path where templates for this extension are stored. + + Note that this path is not connected to any blueprint, so it is up to + the extension to provide extension-unique subdirectories. + """ + return None + def setup_app(self, app): """Called during app startup, after all extensions have loaded."""