I have a simple module that is extracting names from database table. I can display that names in hook_block_view like this:

function example_block_view($delta = '') {

  // $result - an array containing names
  // $names - a string containing names from $result array

  $block = array();

  switch ($delta) {
    case 'xxxxxx':

      $block['title']   = t('Example header');
      $block['content'] = $names;


  return $block;

This will output string with names. But I want to use theme() function for outputting names. So I am continuing by this documentation page: https://www.drupal.org/node/722174

Therefore I edited my code to:

  $block['content'] = array(
    '#theme' => 'example',
    '#header' => 'Example header',
    '#rows' => $result, 

But I can't manage out by documentation what should I do next. What is the simplest example of displaying names by custom theme function in my case? Should I do something in template.php of my theme folder, or should I make an hook_theme() in my module? How could I theme the list of names stored in $result array?

  • 1
    A few things that may or may not be right depending on what you're trying to accomplish...#header should be an array (of column headers), #rows should be an array of rows with the same dimensions as the column header array, and the theme should be '#theme' => 'table',
    – Clive
    Apr 8 '15 at 15:23
  • I completely don't understand :). Header of what columns? I don't have any columns. I though rows are the items I want to display, and what is referencing the 'table'? I don't want a table, I just want names displayed one by one by my own theme function where I can define look of items/names.
    – Incredible
    Apr 8 '15 at 15:26
  • How to write a theme function is a bit too broad question; I am not sure what benefit would be in duplicating here what the Drupal documentation says. Using a module or a theme depends from what you need to do. How you could theme a list depends on what you need, since a theme function can do everything PHP allows.
    – apaderno
    Apr 19 '15 at 7:02

You could use hook_theme to register your theme function, but it sounds like you're wanting output an item list which can be done with theme_item_list:

  $names = array('one','two','three');
  $block['content'] = theme('item_list', array(
    'items' => $names,
    'title' => t('My Names'),

This will fill in your block's content with this:

<div class="item-list">
  <h3>My Names</h3>
  <ul><li class="first">one</li><li>two</li><li class="last">three</li></ul>
  • Thanks. But What if I will need my own theme? Instead of theme('item_list', ...) I'd like to use theme('example_list', ...)'. Where could I define example_list`?
    – Incredible
    Apr 9 '15 at 6:30
  • 1
    Then you'll be generating your own theme functions. Check out this tutorial; it looks like exactly what you're doing.
    – Shawn Conn
    Apr 9 '15 at 7:03

I was experimenting a little bit with examples from @Shawn Conn and managed the minimum that needs to be done is to call theme() function in hook_block_view() and pass variables or arrays into it that will be used in example.tpl.php file:

$block['content'] = theme('example', array('items' => $result));

Then there must be theme_hook in module registered. There is no need for any additional parameters:

function hook_theme() {

    return array('example' => array());     

And finally create example.tpl.php in current theme folder:


<?php foreach($items as $item) { ?>

            <?php print $item['name']; ?>

<?php } ?>


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;

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