Drupal 7 tip: Don't use theme('...', ...) use render arrays
In drupal 7 the recommended method to render output is by using the render api. So no more calling theme(...) directly. Here is a simple example based on the example from the examples module.
<?php
function render_something() {
$render_array = array(
'child' => array(
t('This is some text that should be put together'),
t('This is some more text that we need'),
),
'#separator' => ' | ', // Made up for this theme function.
'#theme' => 'render_example_aggregate',
);
print render($render_array);
}
/**
* Theme function
*/
function theme_render_example_aggregate($variables) {
$output = '';
foreach (element_children($variables['element']['child']) as $item) {
$output .= $variables['element']['child'][$item] . $variables['element']['#separator'];
}
return $output;
}
/**
* Implements hook_theme().
*/
function my_module_theme() {
$items = array(
'render_example_aggregate' => array(
'render element' => 'element',
),
);
return $items;
}
?>
The main reason why the render api exists is it allows for altering by other modules before any html is produced. Also it provides one consistent system to produce any output.
There is a lot more to the render api than only call theme functions. It allows for prefixing, suffixing, caching, theme wrappers, pre rendering and post rendering.
If you want to know more about this check out:
http://chicago2011.drupal.org/sessions/render-api-drupal-7
http://drupal.org/project/examples (renderapi example module)
Add new comment