As much as possible, avoid accessing the Pylons template context :pyc
(or :pytmpl_context
). Use template helper functions <template-helper-functions>
instead. You can use the :py~ckan.plugins.interfaces.ITemplateHelpers
plugin interface to add custom helper functions, see custom template helper functions
.
Always use :py~ckan.lib.helpers.url_for
(available to templates as h.url_for()
) when linking to other CKAN pages, instead of hardcoding URLs like <a href="/dataset">
. Links created with :py~ckan.lib.helpers.url_for
will update themselves if the URL routing changes in a new version of CKAN, or if a plugin changes the URL routing.
Always use :py_
(or, if pluralizaton is needed, :pyungettext
) to mark user-visible strings for localization.
Namespacing helper functions in this way avoids accidentally overriding, or being overriden by, a core helper function, or a helper function from another extension. For example:
/../ckanext/example_theme/v08_custom_helper_function/plugin.py
Namespacing snippets in this way avoids accidentally overriding, or being overridden by, a core snippet, or a snippet from another extension. For example:
snippets/example_theme_most_popular_groups.html
Always use CKAN's custom {% snippet %}
tag instead of Jinja's default {% include %}
tag. Snippets can only access certain global variables, and any variables explicitly passed to them by the calling template. They don't have access to the full context of the calling template, as included files do. This makes snippets more reusable, and much easier to debug.