src/pyams_portal/skin/template.py
changeset 289 fca4100c1733
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_portal/skin/template.py	Thu Aug 05 09:27:48 2021 +0200
@@ -0,0 +1,47 @@
+#
+# Copyright (c) 2015-2021 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+
+"""PyAMS_portal.skin.template module
+
+This module provides a TALES extension which allows to get current template
+configuration and CSS class.
+"""
+
+__docformat__ = 'restructuredtext'
+
+from zope.interface import Interface
+
+from pyams_portal.interfaces import IPortalContext, IPortalPage
+from pyams_skin.layer import IPyAMSLayer
+from pyams_utils.adapter import ContextRequestViewAdapter, adapter_config
+from pyams_utils.interfaces.tales import ITALESExtension
+from pyams_utils.traversing import get_parent
+
+
+@adapter_config(name='template_container_class',
+                context=(Interface, IPyAMSLayer, Interface),
+                provides=ITALESExtension)
+class TemplateContainerClassTALESExtension(ContextRequestViewAdapter):
+    """Template class getter TALES extension"""
+
+    def render(self, context=None, default=''):
+        if context is None:
+            context = self.context
+        result = default
+        portal_context = get_parent(context, IPortalContext)
+        if portal_context is not None:
+            page = IPortalPage(portal_context, None)
+            if page is not None:
+                template = page.template
+                if template is not None:
+                    result = template.css_class
+        return result