Skip to content

Commit

Permalink
Merge pull request #1694 from ckan/1694-document-extra-vars-over-c
Browse files Browse the repository at this point in the history
Document using extra_vars instead of c
  • Loading branch information
nigelbabu committed Jun 12, 2014
2 parents 0703250 + cff83a8 commit baca61b
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion doc/theming/best-practices.rst
Expand Up @@ -9,8 +9,23 @@ Don't use ``c``
---------------

As much as possible, avoid accessing the Pylons template context :py:data:`c`
(or :py:data:`tmpl_context`). Use
(or :py:data:`tmpl_context`). :py:data:`c` is a thread-global variable, which
encourages spaghetti code that's difficult to understand and to debug.

Instead, have controller methods add variables to the :py:data:`extra_vars`
parameter of :py:func:`~ckan.lib.base.render`, or have the templates
call
:doc:`template helper functions <template-helper-functions>` instead.

:py:data:`extra_vars` has the advantage that it allows templates, which are
difficult to debug, to be simpler and shifts logic into the easier-to-test and
easier-to-debug Python code. On the other hand, template helper functions are
easier to reuse as they're available to all templates and they avoid
inconsistencies between the namespaces of templates that are rendered by
different controllers (e.g. one controller method passes the package dict as an
extra var named ``package``, another controller method passes the same thing
but calls it ``pkg``, a third calls it ``pkg_dict``).

You can use the :py:class:`~ckan.plugins.interfaces.ITemplateHelpers` plugin
interface to add custom helper functions, see
:ref:`custom template helper functions`.
Expand Down

0 comments on commit baca61b

Please sign in to comment.