How to render an item list

To use the theme_item_list function, a $variables array needs to be declared which contains an items array, a title which can be empty, a type element (ul or ol) and an attributes array. When a title is passed, a h3 element is added to the HTML markup.

A default implementation of using theme_item_list:


  $variables = array(
    'items' => array(),
    // Leave the title element empty to omit the title.
    'title' => '',
    'type' => 'ul',
    'attributes' => array(),
  );
  
  $variables['items'][] = t('This is a list item.');
  $variables['items'][] = array(
    'data' => t('This is a second list item'),
  );
    
  return theme('item_list', $variables);

Use case example where a link is created for each enabled language on the site:


 $items = array();
  
  $languages = language_list();
  foreach ($languages as $language) {
    if ($language->enabled == 1) {
      $items['items'][] = l('Create ' . $language->name . ' XML', 'xml-export/' . $language->language);
    }
  }
  
  return theme('item_list', $items);

A more complex example using nested list items, classes and ids:


  $variables = array(
    'items' => array(),
    // Leave the title element empty to omit the ul title.
    'title' => '',
    'type' => 'ul',
    'attributes' => array(),
  );
  
  $variables['items'][] = t('This is a list item.');
  $variables['items'][] = array(
    'data' => t('This is a second list item'),
  );
  $variables['items'][] = array(
    'data' => t('This is a nested list item'),
    'id' => 'first-nested',
    'class' => array('parent-item'),
    'children' => array(
       t('This is a nested list item.'),
       t('This is a second nested list item.'),
       array(
         'data' => t('This is a nested nested list item'),
         'id' => 'first-nested-sublevel',
         'class' => array('parent-nested-item'),
         'children' => array(
           t('This is a nested nested list item.'),
           t('This is a second nested nested list item.'),
           // And on and on and on ...
         )
       )
     )
  );
   
  return theme('item_list', $variables);

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.