Have a look at Examples for Developers and the theming_example module that is included in the package.
Register your module's theme implementations.
/** * Implements hook_theme(). * * Defines the theming capabilities provided by this module. */ function theming_example_theme() { return array( 'theming_example_list' => array( // We use 'variables' when the item to be passed is an array whose // structure must be described here. 'variables' => array( 'title' => NULL, 'items' => NULL, ), ), ); }
The theme function.
/** * Theming a simple list. * * This is just a simple wrapper around theme('item_list') but it's worth * showing how a custom theme function can be implemented. * * @see theme_item_list() */ function theme_theming_example_list($variables) { $title = $variables['title']; $items = $variables['items']; // Add the title to the list theme and // state the list type. This defaults to 'ul'. // Add a css class so that you can modify the list styling. // We'll just call theme('item_list') to render. $variables = array( 'items' => $items, 'title' => $title, 'type' => 'ol', 'attributes' => array('class' => 'theming-example-list'), ); $output = theme('item_list', $variables); return $output; }
An example where the theme function is used.
/** * The list page callback. * * An example page where the output is supplied as an array which is themed * into a list and styled with css. * * In this case we'll use the core-provided theme_item_list as a #theme_wrapper. * Any theme need only override theme_item_list to change the behavior. */ function theming_example_list_page() { $items = array( t('First item'), t('Second item'), t('Third item'), t('Fourth item'), ); // First we'll create a render array that simply uses theme_item_list. $title = t("A list returned to be rendered using theme('item_list')"); $build['render_version'] = array( // We use #theme here instead of #theme_wrappers because theme_item_list() // is the classic type of theme function that does not just assume a // render array, but instead has its own properties (#type, #title, #items). '#theme' => 'item_list', // '#type' => 'ul', // The default type is 'ul' // We can easily make sure that a css or js file is present using #attached. '#attached' => array('css' => array(drupal_get_path('module', 'theming_example') . '/theming_example.css')), '#title' => $title, '#items' => $items, '#attributes' => array('class' => array('render-version-list')), ); // Now we'll create a render array which uses our own list formatter, // theme('theming_example_list'). $title = t("The same list rendered by theme('theming_example_list')"); $build['our_theme_function'] = array( '#theme' => 'theming_example_list', '#attached' => array('css' => array(drupal_get_path('module', 'theming_example') . '/theming_example.css')), '#title' => $title, '#items' => $items, ); return $build; }
#headershould be an array (of column headers),#rowsshould be an array of rows with the same dimensions as the column header array, and the theme should be'#theme' => 'table',