[ckan-dev] Jinja extensions and templating
Toby Dacre
toby.okfn at gmail.com
Mon Aug 13 15:56:08 UTC 2012
On 13 August 2012 16:08, Aron Carroll <aron.carroll at okfn.org> wrote:
> With Toby's latest changes the {% ckan_extends %} tag now works
> recursively.
>
> This means that we can use it to extend the core CKAN templates without
> having to create a theme extension. Simply extend the page you want your
> extension added to and include your snippet. Be sure to wrap your snippet
> in a block so that others can remove it if necessary.
>
> For example adding disqus comments looks like:
>
> // ckanext/disqus/templates/package/read.html
> {% ckan_extends %}
>
> {% block primary_content %}
> {{ super() }}
> {% block disqus_comments %}{{ h.disqus_comments() }}{% endblock %}
> {% endblock %}
>
> I've updated the documentation to reflect this.
>
>
Thanks Aron,
Also some thoughts on coding practices with extensions going forward.
We will need to support the following use-cases
1) Old versions of ckan using the extension ckan < 2.0
- these can just use old version of the extension
2) New versions of ckan >= 2.0 using legacy templates
- these need genshi templates if they have pages (for styling etc)
3) New versions of ckan using jinja2 templates
- these need jinja2 templates if they have pages (for styling etc)
To do this I suggest we have 3 template directories (i'm not especially
happy with the names anyone have better suggestions?)
templates - existing genshi templates
templates_new - new jinja2 templates
templates_extend - for templates extending functionality via ckan_extend
the plugins should then setup the correct templates here is some sample
code that does this from the qa extension maybe I should create a toolkit
function to help with this eg p.toolkit.set_templates(config, genshi_dir,
jinja2_dir, extend_dir)
def update_config(self, config):
# check if new templates
templates = 'templates'
if p.toolkit.check_ckan_version(min_version='2.0'):
if not p.toolkit.asbool(config.get('ckan.legacy_templates',
False)):
templates = 'templates_new'
# add the extend templates
p.toolkit.add_template_directory(config, 'templates_extend')
p.toolkit.add_template_directory(config, templates)
Ideally the work done by the plugin should be done in helper function(s)
like aron's example to make adding the functionality to any templates
easy. If all functionality is via helpers then jinja2 templates only
should be used.
I need to try to write these up a bit better here for discussion at least
and we still have time to iron out the details
cheers
Toby
https://github.com/okfn/ckan/blob/a4780ad9a5b53d7837bfbe15f5fcaf908e55ba90/doc/extension-templating.rst#functional-extensions
>
> And as mentioned I've already updated disqus extension.
>
>
> https://github.com/okfn/ckanext-disqus/commit/f5d728c1310fd93b20e5b9bc2edbaef2f62d1698
>
> Now when the disqus plugin is loaded the comments will automatically be
> added to the package/read page.
>
> Cheers,
> Aron
> _______________________________________________
> ckan-dev mailing list
> ckan-dev at lists.okfn.org
> http://lists.okfn.org/mailman/listinfo/ckan-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.okfn.org/pipermail/ckan-dev/attachments/20120813/e93e5972/attachment-0001.html>
More information about the ckan-dev
mailing list