Skip to content

Latest commit

 

History

History
71 lines (50 loc) · 2.55 KB

best-practices.rst

File metadata and controls

71 lines (50 loc) · 2.55 KB

Best practices for writing CKAN themes

Don't use c

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.

Use url_for()

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.

Use _() and ungettext()

Always use :py_ (or, if pluralizaton is needed, :pyungettext) to mark user-visible strings for localization.

Helper function names should begin with the name of the extension

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

Snippet filenames should begin with the name of the extension

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

Use {% snippet %}, not {% include %}

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.