[ckan-dev] Writing a template helper plugin

Giulia Benotto benotto at netseven.it
Mon May 29 14:34:24 UTC 2017


Hi all.
I’m trying to write a template helper plugin, since I need to see resources matching a certain criterion in my home page.
So far, I managed to write my plugin.py file as follows:



import ckan.plugins as plugins
import ckan.plugins.toolkit as toolkit




def most_recent_resources():
   resources = toolkit.get_action('resource_search')(
       data_dict={'query':{'format:csv'}}
   )

   resources = resources[:10]
   return resources


class ThemePlugin(plugins.SingletonPlugin):

   plugins.implements(plugins.IConfigurer)
   plugins.implements(plugins.ITemplateHelpers)

   def update_config(self, config):
       toolkit.add_template_directory(config, 'templates')
       toolkit.add_public_directory(config, 'public')

   def get_helpers(self):
       return {
               'theme_most_recent_resources': most_recent_resources
               }




And to call the function in my html file:


{% for resource in h.theme_most_recent_resources() %}

{{  resource.results.name  }}

{% endfor %}

But somehow I can’t make it work. Any ideas on what I am getting wrong?

Thanks in advance for your help,

Giulia

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.okfn.org/pipermail/ckan-dev/attachments/20170529/487aed9d/attachment-0002.html>


More information about the ckan-dev mailing list