[ckan-changes] [okfn/ckan] a5ea72: new ITemplateHelpers interface

GitHub noreply at github.com
Tue Apr 17 14:22:06 UTC 2012


  Branch: refs/heads/master
  Home:   https://github.com/okfn/ckan
  Commit: a5ea72d9fc155d10a1fdf18814b1ae12b131c9bb
      https://github.com/okfn/ckan/commit/a5ea72d9fc155d10a1fdf18814b1ae12b131c9bb
  Author: Toby <toby.junk at gmail.com>
  Date:   2012-04-17 (Tue, 17 Apr 2012)

  Changed paths:
    M ckan/config/environment.py
    M ckan/plugins/interfaces.py

  Log Message:
  -----------
  new ITemplateHelpers interface


diff --git a/ckan/config/environment.py b/ckan/config/environment.py
index a792b6e..fa91541 100644
--- a/ckan/config/environment.py
+++ b/ckan/config/environment.py
@@ -104,6 +104,20 @@ def find_controller(self, controller):
     else:
         config['pylons.h'] = h
 
+    # extend helper functions with ones supplied by plugins
+    from ckan.plugins import PluginImplementations
+    from ckan.plugins.interfaces import ITemplateHelpers
+
+    extra_helpers = []
+    for plugin in PluginImplementations(ITemplateHelpers):
+        helpers = plugin.get_helpers()
+        for helper in helpers:
+            if helper in extra_helpers:
+                raise Exception('overwritting extra helper %s' % helper)
+            extra_helpers.append(helper)
+            setattr(config['pylons.h'], helper, helpers[helper])
+
+
     ## redo template setup to use genshi.search_path (so remove std template setup)
     template_paths = [paths['templates'][0]]
     extra_template_paths = config.get('extra_template_paths', '')
diff --git a/ckan/plugins/interfaces.py b/ckan/plugins/interfaces.py
index a04d8fd..826e41d 100644
--- a/ckan/plugins/interfaces.py
+++ b/ckan/plugins/interfaces.py
@@ -13,7 +13,7 @@
     'IPackageController', 'IPluginObserver',
     'IConfigurable', 'IConfigurer', 'IAuthorizer',
     'IActions', 'IResourceUrlChange', 'IDatasetForm',
-    'IGroupForm',
+    'IGroupForm', 'ITemplateHelpers',
 ]
 
 from inspect import isclass
@@ -385,6 +385,16 @@ def get_auth_functions(self):
         implementation overrides
         """
 
+class ITemplateHelpers(Interface):
+    """
+    Allow adding extra template functions available via h variable
+    """
+    def get_helpers(self):
+        """
+        Should return a dict, the keys being the name of the helper
+        function and the values being the functions themselves.
+        """
+
 class IDatasetForm(Interface):
     """
     Allows customisation of the package controller as a plugin.


================================================================
  Commit: 94180bd19634374150fe52c9d6f36d4bd02d6b0d
      https://github.com/okfn/ckan/commit/94180bd19634374150fe52c9d6f36d4bd02d6b0d
  Author: Toby <toby.junk at gmail.com>
  Date:   2012-04-17 (Tue, 17 Apr 2012)

  Changed paths:
    M ckan/lib/helpers.py

  Log Message:
  -----------
  pass config varw into h.snippet()


diff --git a/ckan/lib/helpers.py b/ckan/lib/helpers.py
index 7996e7f..0824c15 100644
--- a/ckan/lib/helpers.py
+++ b/ckan/lib/helpers.py
@@ -659,6 +659,7 @@ def snippet(template_name, **kw):
     globs = kw
     globs['h'] = pylons_globs['h']
     globs['c'] = pylons_globs['c']
+    globs['config'] = pylons_globs['config']
     stream = template.generate(**globs)
     for item in PluginImplementations(IGenshiStreamFilter):
         stream = item.filter(stream)


================================================================
Compare: https://github.com/okfn/ckan/compare/1ee9e8e...94180bd


More information about the ckan-changes mailing list