src/pyams_portal/skin/template.py
changeset 289 fca4100c1733
equal deleted inserted replaced
288:390514bce78a 289:fca4100c1733
       
     1 #
       
     2 # Copyright (c) 2015-2021 Thierry Florac <tflorac AT ulthar.net>
       
     3 # All Rights Reserved.
       
     4 #
       
     5 # This software is subject to the provisions of the Zope Public License,
       
     6 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
       
     7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
       
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
       
    10 # FOR A PARTICULAR PURPOSE.
       
    11 #
       
    12 
       
    13 """PyAMS_portal.skin.template module
       
    14 
       
    15 This module provides a TALES extension which allows to get current template
       
    16 configuration and CSS class.
       
    17 """
       
    18 
       
    19 __docformat__ = 'restructuredtext'
       
    20 
       
    21 from zope.interface import Interface
       
    22 
       
    23 from pyams_portal.interfaces import IPortalContext, IPortalPage
       
    24 from pyams_skin.layer import IPyAMSLayer
       
    25 from pyams_utils.adapter import ContextRequestViewAdapter, adapter_config
       
    26 from pyams_utils.interfaces.tales import ITALESExtension
       
    27 from pyams_utils.traversing import get_parent
       
    28 
       
    29 
       
    30 @adapter_config(name='template_container_class',
       
    31                 context=(Interface, IPyAMSLayer, Interface),
       
    32                 provides=ITALESExtension)
       
    33 class TemplateContainerClassTALESExtension(ContextRequestViewAdapter):
       
    34     """Template class getter TALES extension"""
       
    35 
       
    36     def render(self, context=None, default=''):
       
    37         if context is None:
       
    38             context = self.context
       
    39         result = default
       
    40         portal_context = get_parent(context, IPortalContext)
       
    41         if portal_context is not None:
       
    42             page = IPortalPage(portal_context, None)
       
    43             if page is not None:
       
    44                 template = page.template
       
    45                 if template is not None:
       
    46                     result = template.css_class
       
    47         return result