Commits

ole...@7322e99d-02ea-0310-aa39-e9a107903beb  committed f61db9a Draft

ThemeEnginePlugin [refs #11098] : Incorporate Apache(TM) Bloodhound theme's solution for template overrides ... [untested]

Version 2.2.0 release candidate 1

  • Participants
  • Parent commits c45e6e6

Comments (0)

Files changed (4)

         def get_theme_names():
             """Return an iterable of names."""
         
+        def get_template_overrides(name):
+            """(Optional) local changes to specific templates 
+    
+            Return a sequence of tuples (old_html, new_html, function) where
+    
+             old_html::
+               The name of the template overriden by this theme.
+             new_html::
+               The name of the template file replacing the former. 
+             function::
+               Optional callback (or None) to add further data . Signature:
+                       req::
+                           Request object
+                       template::
+                           The value of `old_html` above
+                       data::
+                           Template data, may be modified
+                       content_type::
+                           Reported MIME type
+    
+            since 2.2.0
+            """
+
         def get_theme_info(name):
             """Return a dict containing 0 or more of the following pairs:
         
              description::
-               A breif description of the theme.
+               A brief description of the theme.
              template::
                The name of the theme template file. 
              css::
                The filename of the CSS file.
              htdocs::
-               The folder containg the static content.
+               The folder containing the static content.
              screenshot::
                The name of the screenshot file.
              colors::
 
 Most of these are the same as the simple API above, except ``description``
 is explicit.
+
+Customizing specific views
+~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Since version 2.2.0 it is possible to customize specific views by implementing
+(optional) ``get_template_overrides`` method then . For instance
+the following sample code will change ticket and wiki views using
+custom Genshi templates
+
+    class CustomTheme(...):
+
+        #------8<------ Code omitted ------8<------
+
+        def get_template_overrides(self, name):
+            """Override ticket and wiki templates using custom templates
+            """
+            yield ('ticket.html', 'custom_ticket.html', None)
+            yield ('wiki.html', 'custom_wiki.html', self._modify_wiki)
+
+        def _modify_wiki(self, req, template, data, content_type): 
+            data['var'] = 'value'
+
+Notice that, in sample code, new data will added to render wiki pages. 
 
 setup(
     name = 'TracThemeEngine',
-    version = '2.1.4',
+    version = '2.2.0',
     packages = ['themeengine'],
     package_data = { 'themeengine': ['templates/*.html', 'htdocs/*.js', 'htdocs/*.css', 'htdocs/img/*.gif',
                                      'htdocs/farbtastic/*.png', 'htdocs/farbtastic/*.js', 'htdocs/farbtastic/*.css' ] },

File themeengine/api.py

     def get_theme_names():
         """Return an iterable of names."""
         
+    def get_template_overrides(name):
+        """(Optional) local changes to specific templates 
+
+        Return a sequence of tuples (old_html, new_html, function) where
+
+         old_html::
+           The name of the template overriden by this theme.
+         new_html::
+           The name of the template file replacing the former. 
+         function::
+           Optional callback (or None) to add further data . Signature:
+                   req::
+                       Request object
+                   template::
+                       The value of `old_html` above
+                   data::
+                       Template data, may be modified
+                   content_type::
+                       Reported MIME type
+
+        since 2.2.0
+        """
+
     def get_theme_info(name):
         """Return a dict containing 0 or more of the following pairs:
         

File themeengine/web_ui.py

 from trac.core import *
 from trac.core import ComponentMeta
 from trac.config import BoolOption
+from trac.util.text import exception_to_unicode
 from trac.web.chrome import ITemplateProvider, add_stylesheet, Chrome, add_warning
 from trac.web.api import IRequestFilter
 
                                     .endswith('common/css/trac.css'):
                                 del links['stylesheet'][i]
                                 break
+                if theme:
+                    # Template overrides (since 2.2.0)
+                    overrides = self._get_template_overrides(theme)
+                    template, modifier = overrides.get(template, 
+                                                       (template, None))
+                    if modifier is not None:
+                        modifier(req, template, data, content_type)
             if self.custom_css:
                 add_stylesheet(req, '/themeengine/theme.css')
 
         return template, data, content_type
 
+    # Protected methods
+    def _get_template_overrides(self, theme):
+        overrides = theme.get('template_overrides')
+        if overrides is None:
+            try:
+                overrides = theme['provider'].get_template_overrides(
+                                                    theme['name'])
+            except Exception, e:
+                overrides = {}
+                self.log.warning('Theme %s template overrides : %s',
+                                 theme['name'], 
+                                 exception_to_unicode(e))
+            else:
+                overrides = dict([old, (new, callback)]
+                                 for old, new, callback in overrides)
+            theme['template_overrides'] = overrides
+        return overrides