[ckan-dev] Custom function for custom extension

Hendrik Bunke bunke.hendrik at gmail.com
Tue Jul 9 11:17:34 UTC 2013



--On 2013-07-09 00:24, Jorge Pantoja wrote:
> Hi everyone,
> 
> Im trying to do a custom extension to create a sparql interface in order to
> communicate with a sparql point I have installed (since it is in a local
> network that cannot be accessed from outside). Im using sparql wrapper (
> http://sparql-wrapper.sourceforge.net/) to do that.
> 
> I need to call a python function in the jinja template in order to send the
> query and display the results in the screen.
> 
> However, I dont know how to setup the function correctly in the ckan jinja
> environment, so I can use it in the template.
> 
> Can you help me telling me how to do that correctly?

Hi,

basically you would do the following to get functions in
templates:

-   your PluginClass should implement ckan.plugins.ITemplateHelpers
    (ckan.plugins.implements.ITemplateHelpers, inherit=True)
-   define your helper function
-   define a get_helpers() in your Plugin Class:
    def get_helpers(self):
        return {'your_key': your_above_defined_helper_function }
-   you can then get your function into whatever block inside the
    template:
    {% set your_key = h.your_key() %}
    and then place the result as variable, e.g.
    <p>{{ your_key }}</p>

It's also (shortly) described in the docs:
http://docs.ckan.org/en/ckan-2.0/extensions.html#module-ckan.plugins.interfaces
(search for the TemplateHelpers Interface). Also have a look at
the example extensions.

hope that helps
hendrik


-- 
Dr. Hendrik Bunke
http://gplus.to/hbunke
http://twitter.com/hbunke
http://www.hbxt.org




More information about the ckan-dev mailing list