Updated base header renderer to be able to define a custom template name using request annotations
authorThierry Florac <tflorac@ulthar.net>
Mon, 01 Feb 2021 17:09:09 +0100
changeset 550 d7797830305b
parent 549 3a1b042768da
child 551 7ac97bfbc657
Updated base header renderer to be able to define a custom template name using request annotations
src/pyams_default_theme/features/header/interfaces.py
src/pyams_default_theme/features/header/skin/__init__.py
--- a/src/pyams_default_theme/features/header/interfaces.py	Mon Feb 01 13:48:27 2021 +0100
+++ b/src/pyams_default_theme/features/header/interfaces.py	Mon Feb 01 17:09:09 2021 +0100
@@ -22,6 +22,9 @@
 from pyams_default_theme import _
 
 
+PYAMS_HEADER_TEMPLATE_NAME_KEY = 'pyams.header.template_name'
+"""Request annotations key used to define a custom header renderer template name"""
+
 PYAMS_HEADER_MAIN_CLASS_KEY = 'pyams.header.main_class'
 PYAMS_HEADER_TITLE_MAIN_CLASS_KEY = 'pyams.header.title.main_class'
 PYAMS_HEADER_TITLE_INNER_CLASS_KEY = 'pyams.header.title.inner_class'
--- a/src/pyams_default_theme/features/header/skin/__init__.py	Mon Feb 01 13:48:27 2021 +0100
+++ b/src/pyams_default_theme/features/header/skin/__init__.py	Mon Feb 01 17:09:09 2021 +0100
@@ -25,7 +25,8 @@
 from pyams_default_theme.features.header import SimpleHeaderRendererSettings
 from pyams_default_theme.features.header.interfaces import ISimpleHeaderRendererSettings, \
     PYAMS_HEADER_MAIN_CLASS_KEY, PYAMS_HEADER_NAVIGATION_INNER_CLASS_KEY, \
-    PYAMS_HEADER_NAVIGATION_MAIN_CLASS_KEY, PYAMS_HEADER_TITLE_INNER_CLASS_KEY, \
+    PYAMS_HEADER_NAVIGATION_MAIN_CLASS_KEY, PYAMS_HEADER_TEMPLATE_NAME_KEY, \
+    PYAMS_HEADER_TITLE_INNER_CLASS_KEY, \
     PYAMS_HEADER_TITLE_MAIN_CLASS_KEY
 from pyams_default_theme.features.header.skin.interfaces import IHeaderClass, \
     IInnerNavigationClass, IInnerTitleClass, INavigationClass, ITitleClass
@@ -35,7 +36,7 @@
 from pyams_sequence.interfaces import IInternalReference
 from pyams_sequence.reference import get_reference_target
 from pyams_skin.layer import IPyAMSLayer
-from pyams_template.template import template_config
+from pyams_template.template import get_view_template, template_config
 from pyams_utils.adapter import adapter_config
 from pyams_utils.interfaces import ICacheKeyValue
 from pyams_utils.traversing import get_parent
@@ -133,6 +134,15 @@
     def search_target(self):
         return get_reference_target(self.settings.search_target, request=self.request)
 
+    @property
+    def template_name(self):
+        return self.request.annotations.get(PYAMS_HEADER_TEMPLATE_NAME_KEY, '')
+
+    @property
+    def template(self):
+        template = get_view_template(name=self.template_name)
+        return template(self)
+
     def render(self):
         request = self.request
         context = request.context
@@ -143,7 +153,7 @@
         # don't use cache in preview mode
         preview_mode = request.annotations.get(PREVIEW_MODE, False)
         if preview_mode:
-            return super(BaseHeaderRenderer, self).render()
+            return self.template
         else:
             portlets_cache = get_cache(PORTLETS_CACHE_REGION, PORTLETS_CACHE_NAME)
             cache_key = self.cache_key
@@ -153,7 +163,7 @@
                 result = portlets_cache.get_value(cache_key)
                 logger.debug("Retrieving header content from cache key {0}".format(cache_key))
             except KeyError:
-                result = super(BaseHeaderRenderer, self).render()
+                result = self.template
                 portlets_cache.set_value(cache_key, result)
                 logger.debug("Storing header content to cache key {0}".format(cache_key))
             return result