I’m implementing hook_theme()
in a custom module.
function monitor_chart_theme() { return array( 'chart_monitor' => array( 'template' => 'monitor_chart', 'variables' => array( 'container' => null, 'theme' => null, 'classes' => null, 'chart' => null, ), ), ); }
On the template file, I would like to add additional CSS classes the theme function gets in its parameters. I declared a preprocess function with the following code.
function monitor_chart_preprocess_chart_monitor(&$vars) { if ($vars['classes']) { foreach ($vars['classes'] as $class) { $vars['classes_array'][] = $class; } } }
The template file contains the following code.
<div id="<?php print $container; ?>" class="<?php print $classes; ?>"></div>
If I don’t use the preprocess function, the class attribute of <div>
contains just 'chart-monitor'
, the name of the theme function.
Is there a more elegant way to pass extra variables to a template file?
Sponsored by SupremePR