I'm declaring, on my module, a custom theme function:

function monitor_chart_theme() {
  return array(
    'chart_monitor' => array(
      'template' => 'monitor_chart',
      'variables' => array(
        'container' => null,
        'theme' => null,
        'classes' => null,
        'chart' => null,

On my tpl I would like to add additional classes that I want to pass when I call the function. Now I've declared a preprocess functions like this:

function monitor_chart_preprocess_chart_monitor(&$vars) {
  if ($vars['classes']) {
    foreach ($vars['classes'] as $class) {
      $vars['classes_array'][] = $class;

My tpl is this:

<div id="<?php print $container; ?>" class="<?php print $classes; ?>"></div>

If I don't use the preprocess function, "class" attribute of the div has value "chart-monitor", the name of the theme function.

Is there a more elegant way to pass extra variables to my tpl?

Thanks a lot.


Yes, arguments can be passed when calling the theme function:

$tpl_data = array(
    'data1' => $data1,
    'data2' => $data2,

return theme('my_theme', $tpl_data);

Then in the template $data1 and $data2 will be available.

To add a variable passed here to the default drupal class variable in the template we could add the line:

$class .= " $data1";

before the $class variable is printed.

However I think it's questionable whether or not this method is preferable to your original.

  • This is ok, but if on my tpl I cant print $data1 in "class" attribute, because "class" attribute as the default value, the name of my function (I've updated the post) – arrubiu Feb 26 '14 at 15:44
  • Sorry, I don't think I quite understand what your trying to achieve. – Felix Eve Feb 26 '14 at 15:48
  • I want to add custom class to default classe (default class, added by drupal, is "chart-monitor"). The preprocess method works but I don't know if there is better way – arrubiu Feb 26 '14 at 15:50
  • I've updated my answer but I not convinced it's the best way to do it. Sorry I couldn't help more... – Felix Eve Feb 26 '14 at 15:59
  • I've already try it, it doesn't work. The attribute "class" of my tpl is simply ignored :( Thanks – arrubiu Feb 26 '14 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.