[development] Re-entrant theme(), module theme functions

Barry Jaspan barry at jaspan.org
Fri Apr 27 13:43:00 UTC 2007


Gordon's message about theme('table') motivates me to post an idea
I've been toying with.  I'm sure someone will tell me that the latest
theme patches include this already and if I'd reviewed them like Dries
asked I would know that already... :-)

Part 1.  

It has always seemed odd to me that modules cannot override theme
functions.  Why shouldn't I be able to write (e.g.) "stripe your table
rows and columns with a wide variety of number and color
choices".module?  I can, but the install docs have to say "now copy
this function into template.php unless you aren't using PHPTemplate in
which case..."

I'm sure others have raised this issue before.  Here's my $.02 suggestion:

function hook_theme_<funcname>($a1, ...) {
  return 'html';
}

This would have a lower priority than mytheme_<funcname> and
<themeengine>_<funcname> but higher priority than theme_<funcname>,
and all modules implementing it would run in normal weight/name order.

Part 2.

If we have something like Part 1, we will end up with multiple modules
implementing the same theme function.  If any one of them ends with
"return 'html'", that's the end.  But I might want to just modify the
args slightly and "pass it on" to the next function:

function mymodule_theme_table($hdrs, $rows) {
  $rows[0][0]['#attributes']['class'] .= ' first;
  return theme('table', $hdrs, $rows);
}

Of course, this will result in a stack overflow... unless theme() is
smart enough to remember where it is in the list of theme functions to
call.  Or maybe we could use

  return theme_continue('table', $hdrs, $rows);

for this.  Making theme() or theme_continue() smart enough for this
can be easy and efficient.

Comments?

Barry



More information about the development mailing list